如何使用 Passport 在 Laravel API 中获取当前用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48417970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current user in Laravel API using Passport?
提问by Karthik
I am using passport package for Laravel API Authentication. When I post data I get error:
我正在使用护照包进行 Laravel API 身份验证。当我发布数据时,出现错误:
401 Unauthorized","error":{"error":"Unauthenticated."}.
401 Unauthorized","error":{"error":"Unauthenticated."}。
I use Auth::user()->id;
to get current user id.
我Auth::user()->id;
用来获取当前用户 ID。
How to solve this error?
如何解决这个错误?
回答by Gautam Patadiya
This code helped me:
这段代码帮助了我:
auth()->guard('api')->user()
回答by Rahul Reghunath
The simplest format is auth('api')->user();
最简单的格式是 auth('api')->user();
回答by Tung Nguyen
When you use Auth::user()->id in your function's body. You have not logged in before. Please call login api first to get token then set it to the next API call.
当您在函数主体中使用 Auth::user()->id 时。您之前没有登录。请先调用 login api 获取令牌,然后将其设置为下一个 API 调用。
回答by Haritsinh Gohil
You can get current user in laravel api using passport as below:
您可以使用以下护照在 laravel api 中获取当前用户:
Route::get('/user', function (Request $request) {
return $request->user();
});
but you are getting 401 Error
that means either you are not passing access_token in Authorization header or your access_token has expired so you have to refresh the token,
但是你得到401 Error
这意味着你没有在 Authorization 标头中传递 access_token 或者你的 access_token 已经过期,所以你必须刷新令牌,
You have to pass access_token
in Authorization
header which you have got after successfully logged in.
你必须通过access_token
在Authorization
其中,你得后成功登录头。
Your user route is protected by passport so When calling routes that are protected by Passport, your application's API consumers should specify their access tokenas a Bearertoken in the Authorization headerof their request.
您的用户路由受 Passport 保护,因此在调用受 Passport 保护的路由时,您的应用程序的 API 使用者应在其请求的Authorization 标头中将其访问令牌指定为不记名令牌。
For example, when using the Guzzle HTTP library you can pass it as below:
例如,当使用 Guzzle HTTP 库时,您可以按如下方式传递它:
$response = $client->request('GET', '/api/logout', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
],
]);
from the doc laravel passport passing access token
从 doc laravel 护照传递访问令牌
回答by Evans M.
You need to follow a few steps and ensure they are all correct
您需要遵循几个步骤并确保它们都是正确的
- Follow all the setup instruction for Laravel Passport and ensure the backend side is working correctly
- When making your requests ensure you have passed a correct access token via the authorization header
Authorization: Bearer xxx-token-here-xxx
- 遵循 Laravel Passport 的所有设置说明并确保后端工作正常
- 提出请求时,请确保您已通过授权标头传递了正确的访问令牌
Authorization: Bearer xxx-token-here-xxx
Once you have confirmed the above is done correctly ensure your routes are protected by the api guard. If your routes are not protected then passport won't secure them
一旦您确认上述操作正确完成,请确保您的路由受到 api 防护的保护。如果您的路线不受保护,则护照将无法保护它们
Below example using a group of routes
下面的示例使用一组路由
Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function()
{
Route::get("articles","ArticleController@read");
Route::post("articles","ArticleController@create");
});