Laravel SPA在不需要身份验证中间件的路由上吸引用户 [英] Laravel SPA get user on route that does not require auth middleware
本文介绍了Laravel SPA在不需要身份验证中间件的路由上吸引用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在控制器的构造中,我指定中间件:
In my controller's construct I specify the middleware:
public function __construct()
{
$this->middleware('auth:api')
->except(['index', 'show']);
}
通过auth中间件可以很容易地使用户获得任何路由,方法是:
It's easy to get the user for any routes with the auth middleware by doing:
public function save(Request $request)
{
$user = $request->user();
}
但是我无法弄清楚如何使用户获得允许匿名用户访问它们并且不使用auth中间件的路由:
But I can't work out how to get the user for routes that allow anonymous users to access them and don't use the auth middleware:
public function show(Request $request) {
// $request->user(); - this returns NULL
// How can I get the user here?
}
使用Chome的检查网络"标签时,我可以在请求标头中看到它:
When I use Chome's inspect network tab I can see this in my request header:
授权:承载者kGncvh4IlotxIw2zY80GCnQPTMGyPZKGXntsHzsav2DPvffFfZZG60h74rsE
但是我该怎么做才能从中获取用户对象?
But what would I need to do to get the user object from this?
推荐答案
我通过阅读弄明白了这一点.
神奇的代码行是:
$user = Auth::guard('api')->user();
这篇关于Laravel SPA在不需要身份验证中间件的路由上吸引用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文