Auth :: guard('admin')-> user()返回始终为null

时间:2019-04-18 11:32:19

标签: php laravel laravel-5 eloquent

我定义了一个新的警卫“ Admin”,以便在我的项目中具有多身份验证系统用户和admin。当我登录时就可以了,我将令牌指向其余的api路由。当我使用auth :: guard('Admin')-> user()时,我总是为空

任何建议?

Auth.php:

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

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

    'api' => [
        'driver' => 'passport', 
        'provider' => 'users', 
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ]
],




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

登录方法:

public function ALogin(Request $request){

    //  dd($request);
    //  dd($rrequest->header);

     if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
        $this->connectedAdmin = Auth::guard('admin')->user(); 
        // dd( $connectedAdmin);
         $success['token'] = $this->connectedAdmin->createToken('MyApp')->accessToken; 
         $return = new \stdClass();
         $return->token = $success['token'];
         //dd(self::$connectedAdmin);
         return response()->json($return, 200); 
     }

      // dd(Auth::guard('admin')->user());
     return response()->json("error", 400); 
 }

* /

1 个答案:

答案 0 :(得分:1)

在您的路由文件中,为您指定的多个端点指定防护:

/** routes/api.php */

Route::group(['guard' => 'admin'], function () {

    Route::get('/a-route', 'AController@method');
    // ...

});