如何在Firebase自定义身份验证中填充“标识符"和“提供者"? [英] How to populate `identifier` and `providers` in Firebase custom authentication?
问题描述
我正在对我的Web服务上的用户进行身份验证,然后创建 Firebase自定义令牌通过 php-jwt :
I'm authenticating my users on my web service and then creating Firebase custom token via php-jwt:
// 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");
}
但是,我通过这种方式进行身份验证的用户不会在Firebase控制台的身份验证"面板中显示管理员友好的标识符"和提供商"字段:
But the users that I authenticate this way, don't show the administrator-friendly "Identifier" and "Providers" fields in the "Authentication" panel in the Firebase Console:
前两个是我通过此自定义身份验证过程进行身份验证的用户,最后一个是我直接通过Google进行身份验证的用户.
The first two are users that I authenticated via this custom authentication process, and the last one is a user that I authenticated directly via Google.
如何为通过自定义身份验证创建的用户填充标识符"和提供者"字段?
How can I populate the "Identifier" and the "Providers" fields for users created via custom authentication?
推荐答案
用户"列将仅在附加到用户的信息与登录方法"中的一个或多个给定提供者匹配时显示图标. "部分( https://console.firebase.google.com/project/_ /authentication/providers ).
The "Providers" column will only display an icon if the information attached to a user matches one or more of the the given providers in the "Sign-In Methods" section (https://console.firebase.google.com/project/_/authentication/providers).
自定义提供程序没有明显的图标,并且Firebase不知道在标识符"列中显示什么(UID已在其自己的列末尾).
Custom providers don't have a distinct icon, and Firebase wouldn't know what to display in the "Identifier" column (the UID is already in its own column at the end).
但是,您可以通过预先创建(意味着:首次登录之前)或在用户之后更新用户信息来对列的显示进行一些控制.条目已创建.
However, you do have some control for the display of the columns by creating them in advance (meaning: before signing them in for the first time), or by updating the user information after the user entry has been created.
我准备了一个示例,显示哪个字段组合导致哪个显示:
I prepared an example showing which combination of fields leads to which display:
请注意:
- 显示名称无效:如果仅提供该数据,则该用户将被视为匿名用户.
- 电子邮件+密码与电子邮件/密码"提供商匹配
- 电话号码将始终与电话"提供商匹配
- 即使已禁用提供程序,也会在列中显示匹配的提供程序的图标.
- 电子邮件和电话号码必须唯一.如果您的应用程序允许多个用户使用相同的电子邮件地址/电话号码,那么,如果您仅想查看有关Firebase项目用户的更多信息,就会遇到麻烦.
您可以通过 Firebase Auth REST API 创建和更新用户,但我建议您使用官方的Firebase Admin SDKs SDK之一-如果您想坚持使用PHP,我碰巧知道一个非正式的:文档)(免责声明:我是PHP SDK的维护者:)).
You can create and update users via the Firebase Auth REST API, but I would suggest to use one of the official Firebase Admin SDKs SDK to do it - in case you want to stick to PHP, I happen to know an unofficial one: kreait/firebase-php (Documentation) (Disclaimer: I'm the maintainer of the PHP SDK :) ).
非技术性的说明:我不会太在意Firebase Web控制台中的用户列表:使用Firebase CLI工具或官方(或非官方;)管理SDK之一来创建概述,满足您的需求.
On a non-technical note: I wouldn't bother too much with the user list in the Firebase Web Console: use the Firebase CLI tool or one of the official (or unofficial ;) ) Admin SDKs to create an overview that meets your needs.
您在赏金注释"中提到,您是在Firebase Slack社区中提出此问题的,但没有答案-您可以在#php频道中找到我和其他PHP开发人员.我启用了该频道的通知,因此,如果您还有其他问题,请随时加入.
You mentioned in the Bounty Annotation that you asked this in the Firebase Slack Community without an answer - you can find me and other PHP developers in the #php channel. I enabled notifications for the channel, so please feel free to join if you have further questions.
更新2019-07-25 :SDK现在在 https:上拥有专用的Discord社区: //discord.gg/nbgVfty
仅供参考,这是我用PHP SDK编写的代码,用于为上述屏幕截图创建数据:
FYI, this is the code I wrote with the PHP SDK to create the data for the screenshot above:
<?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',
]));
这篇关于如何在Firebase自定义身份验证中填充“标识符"和“提供者"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!