In your logout() method you could have an
if( auth()->guest() ) {
return response()->json([
'name' => 'Name',
]);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have created an api, and there is a method logout in it. first i log in , and get a token., and then i hit the logour api url , then there i have to fill headers with authorization = bearer + token, then i logged out successfully, but if i again hit the logout url, i got an error something like login doesnot exist, i want to show json response at that time how can i achieve that.
public function logout(){
if (Auth::check()) {
Auth::user()->token()->revoke();
return response()->json(['success' =>'Successfully logged out of application'],200);
}else{
return response()->json(['error' =>'api.something_went_wrong'], 500);
}
}
that is how i do it.
Please or to participate in this conversation.