首页 > 解决方案 > 使用电话和电子邮件注册 Firebase,然后在首次登录时设置密码

问题描述

我有一个移动应用程序,用户通过电子邮件注册和密码正常工作。我想改变它,如下所述。


我想用他的电子邮件和电话号码注册一个用户而不指定密码。这是由系统管理员完成的。然后在第一次登录时,用户应该能够进行电话身份验证,然后设置密码。 firebase 支持这个吗?如果是的话,有人可以指出我该怎么做吗?

标签: androidiosfirebasereact-nativefirebase-authentication

解决方案


使用 Admin SDK创建具有电子邮件和电话号码的用户:

admin.auth().createUser({
  email: 'user@example.com',
  phoneNumber: '+11234567890',
});

然后,用户可以通过客户端 SDK 使用您的应用中的电话号码登录。登录时,您检查是否设置了密码。如果没有,您要求用户通过 设置密码updatePassword。这是使用 JS SDK 的片段:

const phoneNumber = getPhoneNumberFromUserInput();
const appVerifier = new firebase.auth.RecaptchaVerifier(
  'sign-in-button',
  {
    'size': 'invisible',
    'callback': function(response) {
      // reCAPTCHA solved, allow signInWithPhoneNumber.
      onSignInSubmit();
    }
  });
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
  .then((confirmationResult) => {
    // SMS sent. Prompt user to type the code from the message, then sign the
    // user in with confirmationResult.confirm(code).
    ...
    return confirmationResult.confirm(smsCode);
  }).then((userCredential) => {
    // Check if password exists.
    if (!(userCredential.user.providerData[1] &&
          userCredential.user.providerData[1].providerId === 'password')) {
      // Ask user for the new password.
      ...
      // Save the password.
      return userCredential.user.updatePassword(password);
    }
  }).catch((error) => {
    // Error; SMS not sent
    // ...
  });

以上可以在 Firebase 支持的所有平台上完成。


推荐阅读