How to get current user in Laravel API using Passport?

Karthik picture Karthik · Jan 24, 2018 · Viewed 12.2k times · Source

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?

Answer

Gautam Patadiya picture Gautam Patadiya · Oct 15, 2018

This code helped me:

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

for shortline syntax you can use something like this:

auth('api')->user()