The logout method is not available on stateless authentication methods. For example, an API is using a token for authentication. You can't log out there, because the token determines if you're authorized or not.
With sanctum, it's the same thing. It's token-based, so you can't log out the user. Instead, you can decide to remove the tokens of a user. But that only works if you actually have a user. So that would look like this
// Check roles above here
if(!$allowAccess) {
if (auth()->check()) {
// Revoke all tokens...
auth()->user()->tokens()->delete();
// OR
// Revoke the token that was used to authenticate the current request...
$request->user()->currentAccessToken()->delete();
}
return redirect()->route('home');
}
return $next($request);
Note that this is sanctum specific!
Documentation: https://laravel.com/docs/9.x/sanctum#revoking-tokens