For some reason, the only way I have been able to log out is to to put the Auth::logout() method directly into a closure in the api.php route file.
Route::post('logout', fn() => Auth::logout());
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
If using the middleware 'auth:sanctum' I cannot log out of Sanctum using the advised Auth::logout() in the official documentation.
Other issues similar to this suggest doing Auth::guard('web')->logout(), but if you have a test:
Sanctum::actingAs(User::first(), ['*'], 'web');
$response = $this->postJson(
route('logout')
);
dd(auth()->user()->username);
The user still exists in the dump.
The only solution that works is to remove the middleware, but I only want users to hit the logout route if authorized.
Please note, no tokens as it is SPA Sanctum with session/cookies.
Please or to participate in this conversation.