vit*_*web 4 php jwt firebase firebase-authentication
我正在使用Firebase自定义身份验证系统用于使用Ionic制作的移动应用程序,我使用Laravel 5.2作为自定义Auth后端.
当新用户注册时,我在laravel中生成一个令牌(使用firebase/php-jwt)并将其返回到移动应用程序.
在此注册过程中,应用程序接收令牌并使用以下代码在firebase上创建用户:
firebase.auth().signInWithCustomToken($auth.getToken())
.then(function(response) {
$log.debug(response);
})
.catch(function(error) {
$log.debug(error.code + ' - ' + error.message);
});
Run Code Online (Sandbox Code Playgroud)
在后端我使用下面的代码生成令牌:
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");
}
Run Code Online (Sandbox Code Playgroud)
正如您在下面的图像中看到的那样,用户是在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,
)
);
Run Code Online (Sandbox Code Playgroud)
我还做了一些测试,看到传递给firebase的令牌包含所有需要的数据.我在后端使用相同的库(firabase/php-jwt)解码编码的令牌,然后接缝就可以了,但firebase仍然创建没有电子邮件地址的用户.
我不知道我错过了什么:(
我不认为使用自定义令牌是可行的.
node.js Admin SDK允许您创建/ updateUser并为该用户创建自定义令牌.这将是理想的方式,但是当你使用php时,我推荐以下解决方案:
您必须使用客户端js库设置电子邮件:
在客户端应用程序中,signInWIthCustomToken和用户使用提供的电子邮件返回updateEmail,如下所示:
firebase.auth().signInWithCustomToken(token)
.then(function(u??ser) {
return user.updateEmail(email);
}).catch(...)
Run Code Online (Sandbox Code Playgroud)