Laravel 5.3 admin guard not working

[亡魂溺海] 提交于 2019-12-13 17:33:10

问题


I am trying to login from two different model using same login form. I have defined admin guard in config/Auth.php. But when I define admin guard in Foundation/AuthenticateUsers it checks the database table to validate the user but redirects back to same login form.

config/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,
    ],

Foundation/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:


Admin guard redirects to login page because of middleware auth, i think you need to do something like this

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

Read this Protecting Routes, part "Specifying A Guard"



来源:https://stackoverflow.com/questions/41279712/laravel-5-3-admin-guard-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!