Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dingo_d's avatar

Logout user from web app using API call?

I've implemented user authentication on an old project such that a user is authenticated on Laravel.

A user goes to the old app, gets redirected to the Laravel login screen, a passport token is created and the user gets redirected back to the old app.

This works. Now I am implementing the logout functionality, and I am sending a request to my logout API call, but since this request doesn't have anything to do with the web request, I cannot do

$request->session()->invalidate();

Because there is no session associated with this request (API call).

I'm trying to find out is there a way to invalidate a session for a user from my API call.

I can get the user details because I have a bearer token, so $request->user()->token()->revoke(); works.

I tried to find a way to get all the sessions from a user, but no luck so far.

0 likes
6 replies
dingo_d's avatar

I get Method Illuminate\Auth\RequestGuard::logout does not exist. error when adding that, and doing

Auth::guard('web')->logout();

doesn't do anything.

dingo_d's avatar

I have the API logout route, which works fine. It's just that I need a way to kill a user session via API, and I'm not sure how, since I have no session in the API.

dingo_d's avatar

I think I solved my problem in a different way - on logout from the old app, I redirect with certain info (token), and I have a middleware on this route where I can check the token and if everything is ok, just logout the user using Auth::logout()

devondahon's avatar

You can switch to database driven session and then force logout by removing user sessions directly from sessions table:

DB::table('sessions')
    ->whereUserId($request->user()->id)
    ->delete();

Please or to participate in this conversation.