Laravel 5.4 Auth问题

时间:2018-03-12 16:51:15

标签: php laravel laravel-5

我正在尝试使用基本的身份验证来处理新的Laravel安装,并且失败了。我读过Laracasts并倒在S.O.试图找到我的缺点无济于事。因此我在这里。

在我的login.blade.php顶部,我有一个简单的if语句。只需检查用户是否已登录..我正在使用login.blade.php期间创建的基本Laravel make:auth,只需使用此if语句。

@if (Auth::check())
    The user is logged in...
@else
    The user is not logged in
@endif

App/Http/Controllers/Auth/LoginController.php内部非常简单:

<?php

namespace Y3\Http\Controllers\Auth;

use Auth;
use Illuminate\Http\Request;
use Y3\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function logout(Request $request)
    {
        Auth::logout();
        return redirect('/login');
    }
}

所以我尝试重新定向到/home如果他们已登录并且我在其中有一个注销覆盖,只是将其注销并将其重定向回/login - 这在web.php中得到了促进{1}}我把它添加到底部:

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('logout', '\Y3\Http\Controllers\Auth\LoginController@logout');

我的问题

用户永远不会登录&#34; - 它们永远不会重定向到home刀片我的if statement始终显示The user is not logged in。我可以告诉Auth正在做某事,因为如果我输入了错误的用户名或密码 - 它会失败These credentials do not match our records. /logout也可以(我认为),因为它确实重定向到/login刀片。

我没有看到任何语法错误,所以我认为我的逻辑是有缺陷的。有没有人看到我试图这样做的任何明显错误?

3 个答案:

答案 0 :(得分:0)

当您的路由超出web中间件

时会发生这种情况
Route::group( ['middleware' => 'web'],function () {
    Auth::routes();
    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('logout', '\Y3\Http\Controllers\Auth\LoginController@logout');
});

其次,不要忘记清除浏览器cookie和缓存

希望这有帮助

答案 1 :(得分:0)

logout方法更改为以下内容:

/**
 * Logout, Clear Session, and Return.
 *
 * @return void
 */
public function logout()
{
    $user = /Auth::user();
    Log::info('User Logged Out. ', [$user]);
    /Auth::logout();
    Session::flush();
    return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}

并更新构造函数

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

laravel.log文件中的任何错误?

确定。所以这里有一个工作web.php使用auth看起来像L5.6:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

基本LoginController.php的内容如下:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

以下是login.blade.php的外观:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Login') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('login') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col-md-6 offset-md-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }}
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-8 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Login') }}
                                </button>

                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

登录后,它会将您重定向到home.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif

                    You are logged in!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

这些刀片延伸出来的框app.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light navbar-laravel">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                    {{ config('app.name', 'Laravel') }}
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav mr-auto">

                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @guest
                            <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li>
                            <li><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li>
                        @else
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>

                                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>

        <main class="py-4">
            @yield('content')
        </main>
    </div>

    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

答案 2 :(得分:0)

我通过以下方式解决了这个问题:

1)全新安装的Laravel。

2)然后我更改了名称空间。 php artisan app:name Y3

3)更改命名空间后,我检查了composer.json以确保命名空间更改成功,并且我做了composer dump-autoload以获得良好的衡量标准。

4)测试安装后。然后我做了php artisan make:auth

5)在创建的用户迁移文件中,我将表名从用户更改为我想要的表名。

6)然后我转到/config/auth.php并更改了这个:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

      //   'users' => [
      //       'driver' => 'database',
      //       'table' => 'users',
      //   ],
    ],

对此:

'providers' => [
      //  'users' => [
      //      'driver' => 'eloquent',
      //      'model' => App\User::class,
      //  ],

         'users' => [
             'driver' => 'database',
             'table' => 'my_new_table_name',
         ],
    ],

7)然后我跑了php artisan migrate

我成功更改了命名空间,更改了数据库中auth表的名称,并使Auth一直保持工作 - 感谢所有花时间处理我的解决方案。