0

In my Laravel 7 backend I have some api url like:

Route::group(['namespace' => 'Api', 'middleware' => ['auth:sanctum']], function(){
    Route::post('logout/all', 'Auth\LoginController@logoutAll');
    Route::post('logout', 'Auth\LoginController@logout');
    Route::put('profile/{profile}', 'ProfileController@update');
});

The route /logout and /profile/1 work fine, while the route /logout/all gives an error:

{
"error": "Unauthenticated"
}

The token I use is correct because I can use it for others route and it works.
Of course I didn't call the /logout before trying the /logout/all.

I tried to change the controller function of logoutAll, setting it to "logout":

Route::group(['namespace' => 'Api', 'middleware' => ['auth:sanctum']], function(){
    Route::post('logout/all', 'Auth\LoginController@logout');
    Route::post('logout', 'Auth\LoginController@logout');
    Route::put('profile/{profile}', 'ProfileController@update');
});

In this way it works, but this invoke the same function.
That's the code of the function:

public function logout(Request $request)
{
    Auth::user()->tokens()->where('id', Auth::user()->currentAccessToken()->id)->delete();
    return response()->json(['data' => 'User logged out.'], 200);
}

public function logoutAll(Request $request)
{
    //do nothing
    return response()->json(['data' => 'User loggedAll out.'], 200);
}

Help me please.

UPDATE

If I use CURL command this is the output:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The POST method is not supported for this route. Supported methods: GET, HEAD. in file /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php on line 117

#0 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php(103): Illuminate\Routing\AbstractRouteCollection->methodNotAllowed(Array, 'POST')
#1 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php(40): Illuminate\Routing\AbstractRouteCollection->getRouteForMethods(Object(Illuminate\Http\Request), Array)
#2 /Applications/MAMP/htdocs/test-server/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php(162): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
...
enfix
  • 6,680
  • 12
  • 55
  • 80
  • 1
    Often when the problem doesn’t make sense the issue is cache. You have tended to clear your cache with "php artisan config: cache" and "php artisan cache:clear"? – Fellipe Sanches Jun 21 '20 at 17:08
  • I tried "php artisan cache:clear && php artisan config:clear && php artisan config:cache" but without changes – enfix Jun 21 '20 at 17:12
  • This may sound strange, but rename the LoginController controller to something else, and try using something like LoginController2@logoutAll. I've seen the case that the cache persisted ... – Fellipe Sanches Jun 21 '20 at 17:25
  • I have update my question with more details if its can help you – enfix Jun 21 '20 at 17:28
  • I tried to rename (duplicate) LoginController, same error. – enfix Jun 21 '20 at 17:34
  • Do you have a production and development environment in the postman? Is it correct? – Fellipe Sanches Jun 21 '20 at 17:34
  • No ok, I found the problem. In the __constructor() method i have: $this->middleware('guest')->except('logout'); If i comment it works. – enfix Jun 21 '20 at 17:36
  • Seems like, the `logout` and `profile/{id}` pre defined and you just make `logoutAll`? – STA Jun 21 '20 at 17:39

1 Answers1

2

The problem was in the __constructor() method

$this->middleware('guest')->except('logout'); 

I change it to:

$this->middleware('guest')->except(['logout', 'logoutAll']);

Now it works fine.

enfix
  • 6,680
  • 12
  • 55
  • 80
  • Yes, that's why I asked pre defined or not. Cheers!!! – STA Jun 21 '20 at 17:43
  • 1
    glad you figured it out ... can you mark your own answer as the accepted answer so this question is considered closed and answered? – lagbox Jun 21 '20 at 20:59