使用手机号码和密码登录 Firebase(无 OTP)

时间:2021-07-28 04:17:09

标签: android firebase firebase-authentication

我正在尝试制作一个 android 应用程序,使用手机号码和密码进行身份验证 (是的,不是 OTP !!!)。 我已阅读自定义令牌的文章,但没有理解。(this is the link to documentation) 有人可以帮助实现这一点吗? (请使用自定义令牌显示它的实现) 提前谢谢你!!;)

1 个答案:

答案 0 :(得分:0)

自定义身份验证是一种完全自定义的解决方案,需要您自己设置额外的 OAuth 提供程序,而您很可能对此不感兴趣。

相反,您可以通过使用密码 Auth 来实现这一点,只需将项目名称作为域 125132346@myproject-0.com 附加到末尾即可。这确实意味着它与电话验证和您可能认为理想的 OTP 没有直接联系。很酷的是,如果您将来还需要关联手机验证,Firebase 支持 Auth 关联。

密码:https://firebase.google.com/docs/auth/web/password-auth

链接验证:https://firebase.google.com/docs/auth/web/account-linking

更新

要实现自定义令牌,您必须在云服务(例如 Firebase 云函数)上安装 admin-sdk。然后,您必须将用户重定向到该函数或使用用户凭据调用它以生成令牌,这通常是通过另一个 OAuth 提供程序完成的,以防止欺骗。

最小设置如下: 带有 onRequest 的云函数


exports.phoneAuth = functions.https.onRequest((request, response) => {
const uid = request.body.number; // users phone number from the request

return admin
  .auth()
  .createCustomToken(uid)
  .then((customToken) => {
    response.status(200).send(customToken);
  })
  .catch((error) => {
    console.error('Error creating custom token:', error);
  });
});

来源:https://firebase.google.com/docs/auth/admin/create-custom-tokens#create_custom_tokens_using_the_firebase_admin_sdk