错误此路由不支持GET方法。支持的方法:POST。当尝试使用路由而不提供身份验证令牌时

我一直在遇到这个奇怪的错误...我设置了我的laravel护照身份验证,登录有效并提供了令牌。我正在尝试“保护”我的路线,以便只有登录用户才能访问它们。我正在使用'auth:api'中间件,当提供令牌时,一切都会按预期进行,但是当它不提供时,我会不断得到'该路由不支持GET方法。支持的方法:POST。错误,我似乎不知道为什么。

这是我的Authenticate.php

class Authenticate extends Middleware
{
 /**
  * Get the path the user should be redirected to when they are not authenticated.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return string|null
  */
 protected function redirectTo($request)
 {
     if (! $request->expectsJson()) {
         return route('login');
     }
 }
}

这是我设置的路线

 Route::group([
    'prefix' => 'auth'
], function () {
    Route::post('login', 'Auth\AuthController@login')->name('login');
    Route::post('register', 'Auth\AuthController@register');
    Route::group([
        'middleware' => 'auth:api'
    ], function () {
        Route::get('logout', 'Auth\AuthController@logout');
        Route::get('user', 'Auth\AuthController@user');
    });
});

Route::get('check', function () {
    return response()->json([
        'message' => 'YOU'RE IN!'
    ], 200);
})->middleware('auth:api');

这是我的authController

<?php

namespace App\Http\Controllers\Auth;

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

class AuthController extends Controller
{
    //
    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string'
        ]);
        $credentials = request(['email', 'password']);
        if (!Auth::attempt($credentials))
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);
        $user = $request->user();
        $tokenResult = $user->createToken('Personal Access Token');
        $token = $tokenResult->token;
        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addWeeks(1);
        $token->save();
        return response()->json([
            'user' => Auth::user(),
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'expires_at' => Carbon::parse(
                $tokenResult->token->expires_at
            )->toDateTimeString()
        ], 200);
    }
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'fName' => 'required|string',
            'lName' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string'
        ]);
        if ($validator->fails()) {
            $error = $validator->errors()->first();
            return response()->json([
                'message' => $error
            ], 404);
        }
        $user = new User;
        $user->first_name = $request->fName;
        $user->last_name = $request->lName;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->save();
        return response()->json([
            'message' => 'Successfully created user!'
        ], 201);
    }
    public function logout(Request $request)
    {
        $request->user()->token()->revoke();
        return response()->json([
            'message' => 'Successfully logged out'
        ], 200);

        // Auth::logout();
    }
    public function user(Request $request)
    {
        return response()->json($request->user());
    }
}

当我发送带有包含令牌的标头的请求时,该请求有效,并且当我收到错误消息时收到“ YOU'RE IN”消息。我确实尝试将其作为POST和GET请求发送,但实际上并不需要,因为我已将其声明为GET路由。

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117

#0 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(103): Illuminate\Routing\AbstractRouteCollection->methodNotAllowed(Array, 'GET')
#1 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php(40): Illuminate\Routing\AbstractRouteCollection->getRouteForMethods(Object(Illuminate\Http\Request), Array)
#2 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php(162): Illuminate\Routing\AbstractRouteCollection->handleMatchedRoute(Object(Illuminate\Http\Request), NULL)
#3 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(639): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#4 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(628): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#5 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Routing\Router.php(617): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#6 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(165): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#7 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#8 D:\My Workspace\Laravel\awiz\app\Http\Middleware\Cors.php(18): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): App\Http\Middleware\Cors->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(63): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#18 D:\My Workspace\Laravel\awiz\vendor\fruitcake\laravel-cors\src\HandleCors.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 D:\My Workspace\Laravel\awiz\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(140): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#24 D:\My Workspace\Laravel\awiz\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(109): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#25 D:\My Workspace\Laravel\awiz\public\index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#26 D:\My Workspace\Laravel\awiz\server.php(21): require_once('D:\\My Workspace...')
#27 {main}
评论
丑病难医
丑病难医

You're getting that error because your middleware is supposed to redirect to login route i.e return route('login');

The problem is that your login route is POST i.e Route::post('login', 'Auth\AuthController@login')->name('login');

点赞
评论