我正在为使用 Ionic 制作的移动应用程序使用 Firebase 自定义身份验证系统,并且我使用 Laravel 5.2 作为自定义身份验证后端。
当新用户注册时,我在 laravel 中生成一个 token (使用 firebase/php-jwt)并将其返回到移动应用程序。
在此注册过程中,应用程序接收 token 并使用以下代码在 firebase 上创建用户:
firebase.auth().signInWithCustomToken($auth.getToken())
.then(function(response) {
$log.debug(response);
})
.catch(function(error) {
$log.debug(error.code + ' - ' + error.message);
});
在后端,我使用下面的代码生成 token :
private function create_custom_token($uid, $email, $is_premium_account) {
$service_account_email = env('SERVICE_ACCOUNT_EMAIL');
$private_key = env('SERVICE_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,
"email" => $email,
)
);
return JWT::encode($payload, $private_key, "RS256");
}
如下图所示,用户是在 firebase 上生成的,但问题是它没有电子邮件地址,只有一个 UID。
为了方便识别用户,最好有电子邮件地址。
也许有人可以帮忙。谢谢!
更新:
我稍微更改了 create_custom_token 函数的 $payload:
$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,
"email" => $email,
"email_verified" => false,
"claims" => array(
"premium_account" => $is_premium_account,
"email" => $email,
"email_verified" => false,
)
);
我还进行了一些测试,以查看传递给 firebase 的 token 是否包含所有需要的数据。我在后端使用相同的库 (firabase/php-jwt) 对编码 token 进行了解码,接缝没问题,但 firebase 仍然创建没有电子邮件地址的用户。
我不知道我错过了什么:(
最佳答案
我认为使用自定义 token 不可能做到这一点。
node.js Admin SDK 允许您创建/更新用户并为该用户生成自定义 token 。这将是实现此目的的理想方式,但由于您使用的是 php,因此我推荐以下解决方案:
您必须使用客户端 js 库设置电子邮件:
- 生成自定义 token 后,将其连同电子邮件发送给客户端应用程序。无论如何,您已经在发送它,只需将电子邮件与它一起传递即可。
在客户端应用程序中,signInWIthCustomToken 和用户返回的 updateEmail 提供的电子邮件如下:
firebase.auth().signInWithCustomToken(token) .then(function(user) { return user.updateEmail(email); }).catch(...)
关于php - Firebase Auth -> 使用 signInWithCustomToken 创建的用户没有电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42039493/