Laravel Auth :: user()使用guard('admin')

时间:2019-10-02 05:44:57

标签: laravel authentication

我已将防护设置为admin,例如return Auth :: guard('admin')。它可以从表'admins'中获取数据,但是尝试Auth :: user()后返回null

LoginController
===============
public function backendLogin(Request $request)

{   

    $this->validateLogin($request);

    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }


    if ($this->attemptLogin($request)) {

        return $this->sendLoginResponse($request);
    }


    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}
protected function guard()
{
    return Auth::guard('admin');
}

config/auth.php
===============
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

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

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

我尝试了多种类型的

auth()->guard('admin')->attempt($credentials);
Auth::guard('admin')->attempt($credentials);

但仍然无法登录。

1 个答案:

答案 0 :(得分:0)

要登录,请使用

auth('admin')->attempt($credentials))

登录后,您可以像admin卫队一样获得经过身份验证的用户信息

Auth::guard('admin')->user()