Rob*_*Rob 9 php firebase php-jwt
我正在通过我的网络服务验证我的用户,然后通过php-jwt创建Firebase自定义令牌:
// Requires: composer require firebase/php-jwt
use Firebase\JWT\JWT;
// 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");
}
Run Code Online (Sandbox Code Playgroud)
但是,我通过此方式进行身份验证的用户不会在Firebase控制台的"身份验证"面板中显示管理员友好的"标识符"和"提供商"字段:
前两个是我通过此自定义身份验证过程进行身份验证的用户,最后一个是我通过Google直接进行身份验证的用户.
如何为通过自定义身份验证创建的用户填充"标识符"和"提供者"字段?
如果附加到用户的信息与“登录方法”部分(https://console.firebase.google.com/project/ _ / authentication / providers)。
自定义提供程序没有明显的图标,并且Firebase不知道在“标识符”列中显示什么(UID已在其自己的列末尾)。
但是,您确实可以通过预先创建列(即:在首次登录之前)或在创建用户条目之后更新用户信息来控制列的显示。
我准备了一个示例,显示哪个字段组合导致哪个显示:
请注意:
您可以通过Firebase Auth REST API创建和更新用户,但我建议使用一种官方的Firebase Admin SDK SDK来进行操作-如果您要坚持使用PHP,我碰巧知道一个非官方的用户:kreait / firebase-php(文档)(免责声明:我是PHP SDK的维护者:))。
非技术性的说明:我不会太在意Firebase Web控制台中的用户列表:使用Firebase CLI工具或官方(或非官方;)管理SDK之一来创建满足您需要的概述。
您在“悬赏注释”中提到,您是在Firebase Slack社区中提出此问题而没有答案的-您可以在#php频道中找到我和其他PHP开发人员。我为该频道启用了通知功能,因此,如果您还有其他问题,请随时加入。
更新2019-07-25:SDK现在在https://discord.gg/nbgVfty上具有专用的Discord社区
仅供参考,这是我用PHP SDK编写的代码,用于为上述屏幕截图创建数据:
<?php
declare(strict_types=1);
use Kreait\Firebase;
use Kreait\Firebase\Util\JSON;
require_once __DIR__.'/vendor/autoload.php';
$serviceAccount = Firebase\ServiceAccount::fromJsonFile(__DIR__.'/service_account.json');
$firebase = (new Firebase\Factory())
->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',
'displayName' => 'Jérôme Gamez',
'phoneNumber' => '+49-123-7654321',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+password+phone',
'email' => 'jerome@example.de',
'displayName' => 'Jérôme Gamez',
'password' => 'example123',
'phoneNumber' => '+49-321-7654321',
]));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
489 次 |
| 最近记录: |