尝试登录时,我的守卫总是在自定义中间件中返回 null

My guard always returs null in custom middleware when trying to login

我正在尝试进行多重授权,但我似乎无法让它发挥作用。我使用的是自定义中间件而不是 RedirectIfAuthenticated 中间件,但在我的中间件中始终 returns null 但当我使用 RedirectIfAuthenticated 中间件时,它就可以工作了。 这是我的代码

Auth.php

   'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'user' => [
        'driver' => 'session',
        'provider' => 'users'
    ],  
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],

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

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

用户模型

    public $table = 'users';
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */

 protected $guard = 'user';

protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function verifyUser() {
    return $this->hasOne('App\VerifyUser');
}

}

用户登录控制器

    use AuthenticatesUsers;

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

public function showLoginForm() {
    return view('user.login');
}

protected function guard() {
    return Auth::guard('user');
}

public function login(Request $request) {
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|min:8'
    ]);

    if(Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
        // return $this->sendLoginResponse($request);
        // if(Auth::check()) {
            return redirect()->route('userdashboard');
        // }
    }

    return redirect()->back()->withInput($request->only('email', 'remember'));
}

protected function authenticated(Request $request, $user) {   
    echo $user->email_verified_at;
    die();
    // if($user->email_verified_at == null) {
    //     Auth::logout();
    //     return back()->with('warning', 'You need to confirm your email account. We have sent you an email verification code. Please check your email.');
    // }
}

public function logout() {
    session::flush();
    Auth::logout();
    return redirect()->route('userlogin');
}

protected function loggedOut(Request $request)
{
    return redirect('user/login');
}

}

这是我的中间件

    public function __construct()
{
    // Auth::shouldUse('user');
}
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if($guard == 'user' && Auth::guard($guard)->check()){
        return $next($request);
    } else {
        return redirect('home')->with('error', 'You do not hava user access');
    }

    return $next($request);
}

如果你想让你的中间件获得一个参数,你必须在分配中间件时用参数定义它:

Route::get(...)->middleware('user:user');

第一个user是中间件的名字。在 : 之后是将传递给中间件的 handle 方法的参数。所以这个中间件最终会 $guard 成为 user.