[英]Silex SecurityServiceProvider throws 'Identifier “security.authentication_providers” is not defined.'
[英]How to populate `identifier` and `providers` in Firebase custom authentication?
我正在我的网络服务上对我的用户进行身份验证,然后通过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");
}
但是我以这种方式进行身份验证的用户不会在 Firebase 控制台的“身份验证”面板中显示管理员友好的“标识符”和“提供者”字段:
前两个是我通过此自定义身份验证过程进行身份验证的用户,最后一个是我直接通过 Google 进行身份验证的用户。
如何为通过自定义身份验证创建的用户填充“标识符”和“提供者”字段?
如果附加到用户的信息与“登录方法”部分 ( https://console.firebase.google.com/project/ _/身份验证/提供者)。
自定义提供程序没有明显的图标,Firebase 不知道在“标识符”列中显示什么(UID 已经在它自己的列中)。
但是,您确实可以通过预先创建列(意思是:在首次登录之前)或在创建用户条目后更新用户信息来控制列的显示。
我准备了一个示例,显示哪个字段组合导致哪个显示:
请注意:
您可以通过Firebase Auth REST API创建和更新用户,但我建议使用官方 Firebase Admin SDKs SDK 之一来执行此操作 - 如果您想坚持使用 PHP,我碰巧知道一个非官方的: kreait/ firebase-php (文档)(免责声明:我是 PHP SDK 的维护者 :))。
关于非技术性说明:我不会对 Firebase Web 控制台中的用户列表太在意:使用 Firebase CLI 工具或官方(或非官方 ;) Admin SDK 之一来创建满足您需求的概述.
您在 Bounty Annotation 中提到,您在 Firebase Slack 社区中提出了这个问题,但没有得到答复 - 您可以在 #php 频道中找到我和其他 PHP 开发人员。 我为频道启用了通知,所以如果您有其他问题,请随时加入。
仅供参考,这是我用 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',
]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.