Reputation: 4614
I am working on a Laravel 8 API. I use Auth0 for user registration and login.
At registration, I need the user's id returned by Auth0 (as user_id
), in order to insert it into my own users table, in a column called uid
.
Also, once a user is logged in, I need to display the user's data at myapp.test/api/user-profile/show/
for which, also, I need the user_id
.
For this purpose I have the code:
In routes\api.php:
Route::get('/authorization', [UserController::class, 'authorize']);
In the UserController:
public function authorize(){
$appDomain = 'https://' . config('laravel-auth0.domain');
$appClientId = config('laravel-auth0.client_id');
$appClientSecret = config('laravel-auth0.client_secret');
$appAudience = config('laravel-auth0.api_identifier');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "$appDomain/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"client_id\":\"$appClientId\",\"client_secret\":\"$appClientSecret\",\"audience\":\"$appAudience\",\"grant_type\":\"client_credentials\"}",
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
The authorize()
method (taken from a tutorial on the Auth0 website), only returns the access_token info but not the user_id
.
CURLOPT_URL => "$appDomain/oauth/token"
with something, not with what?Upvotes: 0
Views: 1198
Reputation: 183
Please look at this community answer:
https://community.auth0.com/t/how-to-get-user-information-from-the-laravel-api-side/47021/3
Make a request to the Authentication API’s userinfo endpoint to get the user’s profile. https://auth0.com/docs/api/authentication#user-profile
Upvotes: 1
Reputation: 1
to get the id of current login user you can use use Auth ; $user_id = Auth::user()->id ;
it will give your the id current login user id. but if you need complete login user information then you can user
$user = Auth::user() ;
Upvotes: 0