首页 > 解决方案 > 使用手机号码和密码登录 Firebase(无 OTP)

问题描述

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

标签: androidfirebasefirebase-authentication

解决方案


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

相反,您可以通过使用密码 Auth 来实现此目的,只需将项目名称作为 domain 附加到末尾即可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 提供程序完成的,以防止欺骗。

最小设置如下: Cloud Function with 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


推荐阅读