How to authenticate and logging in user in laravel using guard?

501 Views Asked by At

I'm trying to make authentication using guard in Laravel 5.8. Authentication is passed but somehow it's not logging in the user.

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.');
}

When redirected to route /owner/dashboard which is filtered with $this->middleware('owner'), the user will be redirected to login form and get notification that login failed. Is Auth::guard('owner')->attempt($credentials) only authenticating without logging in user?

1

There are 1 best solutions below

4
On

in config/auth.php add this add new guard and the provider for this guard

   '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