I am using Laravel 6.7 with Passport to consume my own API. When I try to logout a user using Auth::logout()
, I get the following error:
Illuminate\Auth\RequestGuard::logout does not exist.
I don't understand why I get such behavior. I haven't used any custom guards. My Auth.php as per the Passport Setup is as follows:
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
This is my AuthenticationController.php where the logout code resides:
/**
* --------------------------------------------------
* Removes the identity of a users login session.
* --------------------------------------------------
* @param Request $request
* @return MessageResource
* --------------------------------------------------
*/
public function logout(Request $request): MessageResource
{
if (Auth::check()) {
Auth::user()->token()->revoke();
}
return new MessageResource(['message' => 'Logout request is successful.']);
}
Is there any way to solve this issue? Any insights would be greatly appreciated. Thanks in advance.
Auth::logout()
is for web guard
.Here, you are using API guard so delete token of authorization it'll automatically logout from you application.
public function logout(Request $request)
{
$request->user()->token()->revoke();
//$request->user()->token()->delete(); for delete.
//Auth::user()->token()->revoke(); same way as revoke user token
return response()->json([
'message' => 'Successfully logged out'
]);
}