I am using Laravel 11 for API with sanctum for authorization. Login works fine but logout always get unauthenticated with Http Status 401.
How to solve this problem and return HTTP status 200 as it suppose to do?
Here are my code and Postman’s API request
AuthController
public function logout(Request $request)
{
$request->user()->currentAccessToken()->delete();
return response()->json([
'message' => 'Logged out successfully',
],200);
}
routes/api.php
Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
Route::middleware('auth:sanctum')->group(function () {
Route::post('/logout', [AuthController::class, 'logout']);
});
Postman
{
"name": "Logout",
"request": {
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Bearer {{access_token}}"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"url": {
"raw": "{{base_url}}/api/logout",
"host": [
"{{base_url}}"
],
"path": [
"api",
"logout"
]
}
},
"response": []
},
Thanks in advance.
I tried adding gaurds but npt working
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],