jwt-auth: The GET method is not supported for this route. Supported methods: POST - Laravel 5.8

The GET method is not supported for this route. Supported methods: POST

I am trying to get a response in dev.site.com.br/api/auth/me and I have the error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\xampp\htdocs\rnlinked\api\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php on line 256

I am using laravel 5.8 and JWT 1.0 The login method works fine, but the me or logout methods doesn’t work.

My environment

| Bug? | yes | Framework | Laravel | Framework version | Laravel Framework 5.8.27 | Package version | “tymon/jwt-auth”: “dev-develop” | PHP version | 7.3.3

Below are my codes:

AuthController.php

<?php

namespace App\Http\Controllers\Api;

use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class AuthController extends Controller
{
    
    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }

    public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->attempt($credentials)) {
            return response()->json(['error' => 'Invalid Login'], 401);
        }

        return $this->respondWithToken($token);
    }

    public function me()
    {
        return response()->json(auth()->user());
    }

    public function logout()
    {
        auth()->logout();

        return response()->json(['message' => 'Successfully logged out']);
    }


    public function refresh()
    {
        return $this->respondWithToken(auth()->refresh());
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => auth()->factory()->getTTL() * 60
        ]);
    }
    
}

In routes/api.php

Route::group([

    'middleware' => 'api',
    'prefix' => 'auth'

], function ($router) {

    Route::post('login', 'Api\AuthController@login')->name('login');
    Route::post('logout', 'Api\AuthController@logout')->name('logout');
    Route::post('refresh', 'Api\AuthController@refresh')->name('refresh');
    Route::post('me', 'Api\AuthController@me')->name('me');

});

I am using postman and I do a POST request on logout or me or refresh and they get the same error. Only the login method works.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 1
  • Comments: 38

Most upvoted comments

Just make sure headers accept value set to application/json. content

and now everything works fine.

Any update to this? I am also experiencing the same issue.

This happened to me today and it was a really silly mistake… I was using http instead of https on my API requests

I’ve done a similar problem, when I created a route in GET and then I went to POST it cleans the laravel cache Try using the following code in your project:

php artisan cache:clear
php artisan route:cache  
php artisan config:cache  
php artisan view:clear 

I’ve done a similar problem, when I created a route in GET and then I went to POST it cleans the laravel cache Try using the following code in your project:

php artisan cache:clear
php artisan route:cache  
php artisan config:cache  
php artisan view:clear 

Thanks for the tip. But it didn’t work for me =/

@pereiracinthiag Try to solve your problem like this.

  1. app > Http > Middleware > Authenticate.php
  • redirectTo func in route(‘login’) to route(‘change route name’)
  1. routes > api.php make route Route::get(‘change route name’), function (){ return response()->json([‘error’ => ‘unauthenticated’]); });

@pereiracinthiag in your routes/api.php you’ve set up all the routes as POST routes… which is why it is not allowing GET. If you want to use GET for me, you’d have to change it to this:

Route::group([

    'middleware' => 'api',
    'prefix' => 'auth'

], function ($router) {

    Route::post('login', 'Api\AuthController@login')->name('login');
    Route::post('logout', 'Api\AuthController@logout')->name('logout');
    Route::post('refresh', 'Api\AuthController@refresh')->name('refresh');
    Route::get('me', 'Api\AuthController@me')->name('me');

});

It’s pretty standard to use POST for logout.

Go to vendor>Laravel>framework>src>illuminate>routing>Router.php

Change line 1154: $this->post(‘logout’, ‘Auth\LoginController@logout’)->name(‘logout’);

to $this->get(‘logout’, ‘Auth\LoginController@logout’)->name(‘logout’);

This solved the problem for me!

just make sure that Headers accept application/json and also make sure that the route grouping is ‘middleware’ => ‘api’, ‘prefix’ => ‘auth’ image

It work for me. Change the ‘middleware’ => ‘api’, to ‘middleware’ => ‘jwt.auth’

Route::get(‘/logout’, ‘Auth\LoginController@logout’);

Can you explain how to change http to https on the API rest. Thanks in advance!

I just replaced http with https in URLs.

I think you haven’t set token while sending a request so you should be sure to send with token for requests such as logout, me, refresh

The problem is with authentication. The routes are fine…See the auth type you are using and provide the credentials for you to hit that Get or Post end point. https://laracasts.com/discuss/channels/laravel/routing-in-laravel-6-methodnotallowedhttpexception

Same error with me, I use JWT too and I got the same error when I sent a request post to route post the problem here is that I already deployed the API in the host website so I can’t use PHP artisan cache: clear

Route::get(‘/logout’, ‘Auth\LoginController@logout’);

work for me in Laravel 8 using : Route::get(‘/logout’, [LoginController::class, ‘logout’]);

@pereiracinthiag I also face similar error “GET method is not supported for this route. Supported methods: POST.” I create the new route for that method/function with a different name and it works.