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');
});
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.