Laravel 5 使用auth(';api';)时->;类'AppServiceProvider'中的用户()返回null
在类Laravel 5 使用auth(';api';)时->;类'AppServiceProvider'中的用户()返回null,laravel-5,Laravel 5,在类AppServiceProvider中使用auth('api')->user()时返回null 此代码config/auth “默认值”=>[ “guard”=>“web”, “密码”=>“用户”, ], ` ` 这是类AppServiceProvider view()->composer('dashboard::layouts.*',函数($view){ dd(auth('api')->user()); $view->with('user',auth('api')->user(); });
AppServiceProvider
中使用auth('api')->user()时返回null
此代码config/auth
“默认值”=>[
“guard”=>“web”,
“密码”=>“用户”,
],
`
`
这是类AppServiceProvider
view()->composer('dashboard::layouts.*',函数($view){
dd(auth('api')->user());
$view->with('user',auth('api')->user();
});代码>您在应用程序中使用会话或令牌吗?如果您使用令牌,您应该在标头请求中包含令牌,因此如果令牌未发送,您将获得空用户,因为用户未经过身份验证,但如果您使用会话,则只需使用Auth::user()
来获得身份验证的用户在标头中发送令牌大多数情况下都可能使用承载前缀,如:承载令牌\u值,若您使用nginx将令牌值添加到头中的授权参数中,请不要忘记将客户端中间件添加到路由中。
要获取经过身份验证的用户信息,可以执行以下操作:
$token = $request->bearerToken();
$tokenId = (new Parser())->parse($token)->getHeader('jti');
$client = Token::find($tokenId);
return $client['user_id'];
为什么要使用auth('api')
?这是自定义函数吗?如果您使用的是laravel默认身份验证系统,那么您应该Auth::user()
i.m使用令牌-并且-i无法在标头中发送令牌您是否以某种方式在标头中发送令牌,非常感谢
$token = $request->bearerToken();
$tokenId = (new Parser())->parse($token)->getHeader('jti');
$client = Token::find($tokenId);
return $client['user_id'];