Laravel Sanctum 未使用邮递员进行身份验证邮递员、身份验证、Laravel、Sanctum

2023-09-07 11:19:39 作者:偏偏記得如此深刻

我是按照 Laravel 官方文档一步一步来的.

I follow the Laravel official document step by step.

当我向 {{host}}/api/login 发送请求时,我可以收到包含令牌的响应.一切都是正确的.

When I send a request to {{host}}/api/login, I can receive the response that includes token. EVerything is correct.

但是当我尝试向 {{host}}/api/user 发送请求时,它总是未经身份验证.

But when I try to send a request to {{host}}/api/user, it is always unauthenticated.

我检查了我的代码几次,我无法修复它.

I checked my code several times, I cannot fix it.

在我的 .env 文件中,我设置如下,我的后端主机是 http://laravel_8_api.test

In my .env file, I set as following, my backend host is http://laravel_8_api.test

SESSION_DOMAIN=.laravel_8_api.testSANCTUM_STATEFUL_DOMAINS=.laravel_8_api.test

SESSION_DOMAIN=.laravel_8_api.test SANCTUM_STATEFUL_DOMAINS=.laravel_8_api.test

我怎样才能让它工作?请帮我.邮递员请求截图

How can I make it work? Please help me. The postman request screenshot

代码在此链接https://github.com/ramseyjiang/laravel_8_api"中

The code is in this link "https://github.com/ramseyjiang/laravel_8_api"

推荐答案

//api.php

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

中间件应该是 auth:sanctum 而不是 of auth:api

middleware should be auth:sanctum instead of auth:api