tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.23k stars 1.55k forks source link

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

Open pereiracinthiag opened 5 years ago

pereiracinthiag commented 5 years ago

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.

lucashtc commented 5 years ago

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 
pereiracinthiag commented 4 years ago

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 =/

lindyhopchris commented 4 years ago

@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.

meteorpark commented 4 years ago

@pereiracinthiag Try to solve your problem like this.

  1. app > Http > Middleware > Authenticate.php

    • redirectTo func in route('login') to route('change route name')
  2. routes > api.php make route Route::get('change route name'), function (){ return response()->json(['error' => 'unauthenticated']); });

Mknan-fms commented 4 years ago

@pereiracinthiag I came across this issue before in 5.8, consider specifying request method type in your html tag <html method="POST"> rather than using @method('POST').

ayoub-bousetta commented 4 years ago

Error still not fixed

meesudzu commented 4 years ago

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

julyamba commented 4 years ago

i have the same error but in my case, the error only occurs when i Clicked the URL and pressing enter. the error shows, but if hitting f5 the page refreshes normally no error at all. how to fix this error please help. im using POST method.

far1din commented 4 years ago

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!

julyamba commented 4 years ago

@konte21 Sir still doesn't work, maybe because my vendor folder greyed and when i changed the line 1154 it didn't take effect.

my scenario is like this, i have blade template and has a form and uses post method action="home/something", after clicking submit, the current page is in /home/something and displays some information but when i click the url bar and the url is all selected then pressing button enter the "The GET method is not supported for this route. Supported methods: POST." shows.

msaguindang commented 4 years ago

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

beitomartinez commented 4 years ago

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

RajputBoy commented 4 years ago

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST.

I am using the Stripe payment gateway when I click on Pay then I am getting this error also- my controller cose here- form route-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request; use Illuminate\Support\Facades\Redirect; use Session; use Stripe; use DB;

class StripePaymentController extends Controller { /**

  • success response method.
  • @return \Illuminate\Http\Response */ public function stripe(Request $request) {

    $session_id=$_POST['session_id']; $class_id=$_POST['class_id']; $booking_id=$_POST['booking_id'];

    return view('stripe',compact('session_id','class_id','booking_id'));

    }

    /**

  • success response method.
  • @return \Illuminate\Http\Response */ public function stripePost(Request $request) {

    //print_r($_POST); die;

    Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
    
    Stripe\Charge::create ([
            "amount" => 100 * 100,
            "currency" => "usd",
            "source" => $request->stripeToken,
            "description" => "Test payment from itsolutionstuff.com.",
            'shipping' => [
            'name' => 'Jenny Rosen',
            'address' => [
              'line1' => '510 Townsend St',
              'postal_code' => '98140',
              'city' => 'San Francisco',
              'state' => 'CA',
              'country' => 'US'
            ],
          ],
    ]);
    
    Session::flash('success', 'Payment successful!');
    
    return redirect()->back();

    } }

?>

onyashed commented 4 years ago

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

islamMaruf commented 4 years ago

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

and now everything works fine.

kanfur commented 4 years ago

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

AtomCutter commented 4 years ago

@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.

egulhan commented 4 years ago

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

This solved my problem! Thanks!

jordillps commented 4 years ago

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

egulhan commented 4 years ago

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

I just replaced http with https in URLs.

jordillps commented 4 years ago

Thanks for your response. In fact,I am using https. The error appears when I do the laravel the validation of the form. So, at the end I decided do the validation using Jquery.

ausafmughal commented 3 years ago

@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.

This Solution worked for me thanks

marcelolaias commented 3 years ago

Try this, go to file vendor/laravel/framework/src/Illuminate/Routing/CompiledRouteCollection, line 159

Replace the code with the following: $trimmedRequest->server->set('REQUEST_URI', $parts[0].(isset($parts[1]) ? '?'.$parts[1] : ''));

It is not a good choice to change the vendor codes, but until the correction comes out this may be an alternative.

parallela commented 3 years ago

Same issue here.

Here's my stackoverflow question: https://stackoverflow.com/questions/63389765/api-405-method-not-allowed-on-laravel-7-with-tymon-jwt-auth?noredirect=1#comment112089868_63389765

Sadeesha-Dias commented 3 years ago

@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');

});

This worked for me too. Thanks

poria92 commented 3 years ago

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

miguel456 commented 3 years ago

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

That won't work very well for Laravel 8, see Basic Routing

miladzarei92 commented 3 years ago

Route::get('/logout',[LoginController::class, 'logout'] );

YoussefKafa commented 3 years ago

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

gotomark commented 3 years ago

This worked for me class AuthController extends Controller { /**

iammohsinar commented 3 years ago

i got same error on my hosting which is on linux i change my URL from http://your-domain.com/api/post-test/ to http://your-domain.com/api/post-test i just remove / and it works like charm

webliberty74000 commented 3 years ago

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

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

frddl commented 3 years ago

just make sure that Headers accept application/json

@YoussefKafa @islamMaruf I have experienced the similar issue, can you please explain the reasoning behind it? I can not fully understand why not having an Accept header explicitly set to application/json triggers MethodNotAllowedException, it doesn't sound logical to me.

osamax2009 commented 2 years ago

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 

this worked for me thankx

Aclaputra commented 2 years ago

@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.

this one solved my problem, thanks a lot :')

ramsesmoreno commented 1 year ago

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!

I do not recommend change the code inside vendor folder, it will be replaced on any update or install

Samux0112 commented 1 year ago

i have the same problem whit laravel 10 because i try to catch the local connection but i have the problem abaout the method is not support

Bdiwy commented 5 months ago

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