Skip to content
Advertisement

Laravel – Error when calling my logout route

I did the installation of laravel as sent by the basic authentication guide with auth, my login works and returns me a token, but when I try to log out it returns me the following error:

HTTP/1.0 404 Not Found
Host: 127.0.0.1:8000
Date: Wed, 09 Dec 2020 19:33:08 GMT, Wed, 09 Dec 2020 19:33:08 GMT
Connection: close
X-Powered-By: PHP/7.2.24-0ubuntu0.18.04.7
Cache-Control: no-cache, private
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Token-Auth, Authorization
    
{

  "message": "",
  "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
  "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php",
"line": 43,
  "trace": [
   ...
   {
  "file": "/home/shinier01/Projetos/Condivest/api-condinvest/api/app/Http/Middleware/Cors.php",
  "line": 18,
  "function": "Illuminate\Pipeline\{closure}",
  "class": "Illuminate\Pipeline\Pipeline",
  "type": "->"
  },
 ...
  ]
}

this error in my middlaware where it was for him to do the control with the token, but I think I’m doing something wrong, it looks like this:

<?php

namespace AppHttpMiddleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Token-Auth, Authorization');
    }
}

so is my logout route set up:

Route::middleware('auth:api')->get('/user', function (Request $request) {
    Route::get('/logout', 'AuthApiAuthController@logout')->name('logout.api');
});

and this way I’m making my request:

GET http://127.0.0.1:8000/api/user/logout HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJ0eXA...6c

I’ve tried both putting the “Bearer” and taking the same error.

Does anyone know what I’m doing wrong? or what I forgot to install, the one to put in my code?

I appreciate any tip, help or answer right now.

Advertisement

Answer

My mistake was in how I was assembling the route in the api, both the route in the request, that’s right:

Route::group(['middleware' => ['cors', 'json.response', 'auth:api']], function () {
    Route::get('/auth/logout', 'AuthApiAuthController@logout')->name('logout.api');

    Route::post('/group/store', 'GroupController@store')->name('create.group.api');
});

Request:

GET http://127.0.0.1:8000/api/auth/logout HTTP/1.1
Content-Type: application/json
Authorization: Bearer eyJ...dulTQe7LfH26c
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement