when i call route /api/logout
i getting Unauthenticated
, i already have tokens from my /api/login
methods, also when i try run demo request with same middleware
it works perfect
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
route ( in api.php with api prefix )
Route::middleware('auth:api')->post('/logout', 'PassportAuthController@logout')->name('api.auth.logout');
simple logout function
public function logout( )
{
auth()->user()->tokens->each(function ($token, $key) {
$token->delete();
});
return response()->json('Logged out', 200);
}
auth()
is a helper function where you cant pass parameter of the auth you use
specially if you use multiple auth systems so you should determine which auth ?
so to be sure that you logout from api auth not the default auth write
public function logout( )
{
auth('api')->user()->tokens->each(function ($token, $key) {
$token->delete();
});
return response()->json('Logged out', 200);
}