如何使用Guard在laravel中验证和登录用户?

时间:2019-07-18 01:36:23

标签: laravel laravel-5 laravel-authentication laravel-guard

我正在尝试使用Laravel 5.8中的Guard进行身份验证。身份验证已通过,但不知何故未登录用户。

public function login(Request $request)
{
  $email = $request->email;
  $password = $request->password;

  $credentials = $request->only('email','password');

  if (Auth::guard('owner')->attempt($credentials,$request->remember)){

     //echo "Authentication is passed";

     return redirect()->intended('/owner/dashboard');
  }

  return redirect('/owner')->with('error','Login failed.');
}

当重定向到用/owner/dashboard过滤的路由$this->middleware('owner')时,用户将被重定向到登录表单并收到登录失败的通知。 Auth::guard('owner')->attempt($credentials)是否仅在不登录用户的情况下进行身份验证?

1 个答案:

答案 0 :(得分:0)

在config / auth.php中添加此 添加新的警卫和该警卫的提供者

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    ```
    ```
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'owner' => [
            'driver' => 'eloquent',
            'model' => App\Owner::class,
        ],

    ],

    ```
    ```
      'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'owner' => [
            'provider' => 'owner',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
    ```
    Follow this article
    https://medium.com/@sagarmaheshwary31/laravel-multiple-guards-authentication-setup-and-login-2761564da986