2

I have set the guard as admin like return Auth::guard('admin'). it can get data from the table 'admins' but after attempt Auth::user() return null

LoginController =============== public function backendLogin(Request $request) { $this->validateLogin($request); if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); } protected function guard() { return Auth::guard('admin'); } config/auth.php =============== 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ], 

I have tried many type of

auth()->guard('admin')->attempt($credentials); Auth::guard('admin')->attempt($credentials); 

but still cannot login.

8
  • try this auth('admin')->attempt($credentials)) Commented Oct 2, 2019 at 5:48
  • @ZainFarooq still same, because i have a middleware auth, it kick me out to login page after login.. i tried dd(auth::user()) after login using auth('admin') but it return null Commented Oct 2, 2019 at 5:52
  • Have you tried auth::user('admin')->id? Commented Oct 2, 2019 at 5:53
  • After login you should get auth info like this Auth::guard('admin')->user() Commented Oct 2, 2019 at 5:55
  • 1
    @ZainFarooq okay thx you very much! Commented Oct 2, 2019 at 7:32

1 Answer 1

3

For getting logged in you should use

auth('admin')->attempt($credentials)) 

After login you can get authenticated user's info like this for admin guard

Auth::guard('admin')->user() 
Sign up to request clarification or add additional context in comments.

2 Comments

Great Anwser! This Solved My Problem!
Nice. Its great to help you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.