简体   繁体   English

RouteNotFoundException [登录] Laravel Sanctum

[英]RouteNotFoundException [login] Laravel Sanctum

I am currently learning Laravel and using Sanctum to perform authentication.我目前正在学习 Laravel 并使用 Sanctum 进行身份验证。

I have a route working /register and /login and I am trying to create /me endpoint that's protected using auth:sanctum which as a test just returns the authenticated user.我有一个工作 /register 和 /login 的路由,我正在尝试创建 /me 使用 auth:sanctum 保护的端点,它作为测试只返回经过身份验证的用户。

In my api.php I have the following:在我的 api.php 我有以下内容:

Route::post('/auth/register', [UserController::class, "register"]);

Route::post('/auth/login', [UserController::class, "login"]);

Route::middleware('auth:sanctum')->get('/me', function(){
    return auth()->user();
});

In my UserController class I have the following:在我的 UserController class 我有以下内容:

class UserController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function register(Request $request)
    {
        $user = User::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'password' => bcrypt($request['password'])
        ]);

        return response([
            'success' => $user->createToken('API Token')->plainTextToken
        ]);
    }

    public function login(Request $request)
    {
        $attr = $request->validate([
            'email' => 'required|string|email|',
            'password' => 'required|string|min:6'
        ]);

        if (!Auth::attempt($attr))
        {
            return response('Credentials not found', 401);
        }

        return response([
            'token' => auth()->user()->createToken('API Token')->plainTextToken
        ]);
    }

    public function logout()
    {
        auth()->user()->tokens()->delete();

        return [
            'message' => 'Tokens Revoked'
        ];
    }
}

The /login and /register routes work fine, however, when I attempt to use the /logout or /me route which is using auth:sanctum middleware, I get the following error: /login 和 /register 路由工作正常,但是,当我尝试使用使用 auth:sanctum 中间件的 /logout 或 /me 路由时,我收到以下错误:

Symfony\Component\Routing\Exception\RouteNotFoundException
Route [login] not defined.

Everything I've Google'd seem to show that I've implemented it correctly, so I'm not sure what I'm missing.我在谷歌上搜索到的所有东西似乎都表明我已经正确地实现了它,所以我不确定我错过了什么。

I managed to figure out the problem with some help from @LessMore.在@LessMore 的帮助下,我设法找出了问题所在。

I think most of the problem the auth.php being wrong.我认为大部分问题是 auth.php 是错误的。 Under config/auth.php, under the api section change the driver from token to session, so it should be as follows:在 config/auth.php 下,在 api 部分下,将驱动程序从令牌更改为 session,因此应如下所示:

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'session',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

The other thing was I was forgetting to add the Authorization header with the bearer token that is returned on the login and to put Accept application/json header.另一件事是我忘记添加授权 header 和登录时返回的不记名令牌,并放入 Accept application/json header。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM