Laravel圣所未经验证 [英] Laravel sanctum unauthenticated
问题描述
我在我的项目中使用Laravel圣所,并以angular为前端.从第二个api请求中获取未经身份验证的信息.请让我知道我要去哪里了
I am using Laravel sanctum in my project with angular as frontend. Getting unauthenticated from the second api request. Please let me know where am I going wrong
前端-> 127.0.0.1:4200
Frontend-> 127.0.0.1:4200
后端->本地主机:8888
Backend-> localhost:8888
.env配置
SESSION_DOMAIN =本地主机SANCTUM_STATEFUL_DOMAINS = 127.0.0.1
SESSION_DOMAIN=localhost SANCTUM_STATEFUL_DOMAINS=127.0.0.1
将中间件 auth:sanctum 添加到api.php中的路由组
Added middleware auth:sanctum to the routes group in api.php
推荐答案
对于任何未经身份验证的错误,请确保您遵循以下步骤.
For anyone having an Unauthenticated error, please ensure you follow these steps.
- 向/sanctum/csrf-cookie发送发帖请求
- 将发布请求发送到Web路由/login以进行身份验证
此步骤之后,您将通过API路由中的 auth:sanctum 中间件成功进行身份验证.
After this step, you will be successfully authenticated by auth:sanctum middleware in the API route.
其他需要注意的事项:
- 确保将您的SESSION_DOMAIN设置为 localhost
- SANCTUM_STATEFUL_DOMAIN设置为您的子域/SPA,端口为例如localhost:8000
对于原始问题,请确保您维护相同的域.我的意思是两者都使用localhost.并设置SANCTUM_STATEFUL_DOMAIN = localhost:4200
For the original question please ensure you maintain same domain. I mean use localhost for both. And set SANCTUM_STATEFUL_DOMAIN = localhost:4200
已编辑
还设置了SESSION_DRIVER
Also set SESSION_DRIVER
这篇关于Laravel圣所未经验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!