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?
This code helped me:
auth()->guard('api')->user()
The simplest format is auth('api')->user();
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