【发布时间】:2022-02-02 03:43:47
【问题描述】:
我正在开发 Laravel 8 API。我使用 Auth0 进行用户注册和登录。
在注册时,我需要 Auth0 返回的用户 ID(如 user_id),以便将其插入到我自己的 users 表中,位于名为 uid 的列中。
另外,一旦用户登录,我需要在myapp.test/api/user-profile/show/ 显示用户的数据,为此我还需要user_id。
为此,我有代码:
在routes\api.php:
Route::get('/authorization', [UserController::class, 'authorize']);
在用户控制器中:
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;
}
}
问题
authorize() 方法(取自 Auth0 网站上的教程)仅返回 access_token 信息,但不返回 user_id。
问题
- 如何获取当前用户的user_id?
- 我想我需要用一些东西代替
CURLOPT_URL => "$appDomain/oauth/token",而不是用什么?
【问题讨论】:
-
你为什么在 Laravel 中使用 curl?一般使用 Guzzle 或 Laravel 的内置 HTTP 客户端:laravel.com/docs/8.x/http-client。此外,构建自己的 JSON 是个坏主意。
-
@miken32 你会怎么做?
-
Guzzle 或 Laravel HTTP 客户端都接受要发布的数据数组,并为您构建 JSON。 laravel.com/docs/8.x/http-client#request-data
-
@miken32 请帮我处理一下 this question。谢谢!
标签: php laravel laravel-8 auth0