php – 如何在Firebase自定义身份验证中填充`identifier`和`prov
|
我在我的网络服务上验证我的用户,然后通过
php-jwt创建
Firebase custom token:
// Requires: composer require firebase/php-jwt
use FirebaseJWTJWT;
// Get your service account's email address and private key from the JSON key file
$service_account_email = ...;
$private_key = ...;
function create_custom_token($uid,$is_premium_account) {
global $service_account_email,$private_key;
$now_seconds = time();
$payload = array(
"iss" => $service_account_email,"sub" => $service_account_email,"aud" => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit","iat" => $now_seconds,"exp" => $now_seconds+(60*60),// Maximum expiration time is one hour
"uid" => $uid,"claims" => array(
"premium_account" => $is_premium_account
)
);
return JWT::encode($payload,$private_key,"RS256");
}
但是,我通过此方式进行身份验证的用户不会在Firebase控制台的“身份验证”面板中显示管理员友好的“标识符”和“提供商”字段: 前两个是我通过此自定义身份验证过程进行身份验证的用户,最后一个是我通过Google直接进行身份验证的用户. 如何为通过自定义身份验证创建的用户填充“标识符”和“提供者”字段?
如果附加到用户的信息与“登录方法”部分(
https://console.firebase.google.com/project/_/authentication/providers)中的一个或多个给定提供者匹配,则“提供者”列将仅显示图标.
自定义提供程序没有明确的图标,Firebase不知道在“标识符”列中显示什么(UID最后已经在其自己的列中). 但是,您可以通过预先创建列来显示列的显示(意味着:在第一次签名之前),或者在创建用户条目后更新用户信息. 我准备了一个示例,显示哪个字段组合可以显示哪个: 请注意: >显示名称无效:如果它是唯一提供的数据,则认为该用户是匿名的. 你可以通过Firebase Auth REST API创建和更新用户,但我建议使用官方的Firebase Admin SDKs SDK之一 – 如果你想坚持PHP,我碰巧知道一个非官方的:kreait/firebase-php(Documentation)(免责声明:我是PHP SDK的维护者:)). 在非技术性说明中:我不会过多地使用Firebase Web控制台中的用户列表:使用Firebase CLI工具或其中一个官方(或非官方;)管理SDK来创建满足您需求的概述. 你在Bounty Annotation中提到过你在Firebase Slack社区中没有回答的问题 – 你可以在#php频道找到我和其他PHP开发人员.我为频道启用了通知,如果您有其他问题,请随时加入. 仅供参考,这是我用PHP SDK编写的代码,用于为上面的屏幕截图创建数据: <?php
declare(strict_types=1);
use KreaitFirebase;
use KreaitFirebaseUtilJSON;
require_once __DIR__.'/vendor/autoload.php';
$serviceAccount = FirebaseServiceAccount::fromJsonFile(__DIR__.'/service_account.json');
$firebase = (new FirebaseFactory())
->withServiceAccount($serviceAccount)
->create();
$auth = $firebase->getAuth();
// Remove all users
foreach ($auth->listUsers() as $user) {
$auth->deleteUser($user->uid);
}
// Simulate custom auth
$ct = $auth->createCustomToken('a-custom-auth');
$r = $auth->getApiClient()->exchangeCustomTokenForIdAndRefreshToken($ct);
echo JSON::prettyPrint($auth->getUser('a-custom-auth'));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'displayname-only','displayName' => 'Jér?me Gamez',]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-only','email' => 'jerome@example.org',]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-and-password','email' => 'jerome@example.com','password' => 'password'
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'phone-only','phoneNumber' => '+49-123-1234567',]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+phone','email' => 'jerome@example.net','phoneNumber' => '+49-123-7654321',]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+password+phone','email' => 'jerome@example.de','password' => 'example123','phoneNumber' => '+49-321-7654321',]));
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
