Laravel 5.3管理员后卫无法正常工作

时间:2016-12-22 09:25:18

标签: php laravel laravel-5 laravel-5.3

我正尝试使用相同的登录表单从两个不同的模型登录。我在config / Auth.php中定义了admin guard。但是当我在Foundation / AuthenticateUsers中定义admin guard时,它会检查数据库表以验证用户,但重定向回相同的登录表单。

配置/ auth.php

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

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

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

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

基金会/ AuthenticatUsers

protected function guard()
   {
       return Auth::guard('admin');
   }
public function login(Request $request)
  {
   $credentials = $this->credentials($request);

    if (Auth::guard('web')->attempt($credentials, $request-    >has('remember'))) {
        return $this->sendLoginResponse($request);
    }
    elseif(Auth::guard('admin')->attempt($credentials, $request->has('remember')))
    {
    return $this->sendLoginResponse($request);
    }
  }

1 个答案:

答案 0 :(得分:0)

由于中间件身份验证,管理员重定向重定向到登录页面,我认为你需要做这样的事情

public function __construct()
{
    $this->middleware('auth:admin');
}

阅读此Protecting Routes,部分"指定警卫"