How to get current user in Laravel API using Passport?

你离开我真会死。 提交于 2019-12-04 22:56:53

问题


I am using passport package for Laravel API Authentication. When I post data I get error:

401 Unauthorized","error":{"error":"Unauthenticated."}.

I use Auth::user()->id; to get current user id.

How to solve this error?


回答1:


This code helped me:

auth()->guard('api')->user()



回答2:


The simplest format is auth('api')->user();




回答3:


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.



来源:https://stackoverflow.com/questions/48417970/how-to-get-current-user-in-laravel-api-using-passport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!