1
votes

I have already set up Laravel passport for user authentication.

I was trying to use a GET API to retrieve the user info. After the token is authenticated, all users info are returned. What I expect is only authenticated user info is returned

The problem is $user = Auth::user(), Auth::user returned all user info.

Besides, I am using Nginx, not sure if it will cause the problem.

Thanks for anyone who can help!

UserController.php

public function showAll()  {
                $user = Auth::user();
                return response()->json(['data' => $user], 200, [], JSON_NUMERIC_CHECK);
}

routes/api.php

Route::post('social_auth', 'Api\Auth\SocialAuthController@socialAuth');

Route::middleware('auth:api')->group(function () {
        Route::get('user_info', 'Api\UserController@showAll');
});
1

1 Answers

1
votes

Try this

Auth::guard('api')->user()->id;

public function showAll()  {
                $user = Auth::guard('api')->user()->id;      
                return response()->json(['data' => $user], 200, [], JSON_NUMERIC_CHECK);
}