【问题标题】:Firebase how to link account created with phoneNumberFirebase如何将创建的帐户与电话号码相关联
【发布时间】:2020-12-07 13:49:45
【问题描述】:

✅我可以让用户使用verifyPhoneNumber 更新他们的手机号码并更新currentUser.updatePhoneNumber

❌ 当我允许通过电话登录时,我的问题出现了,并且 新用户 尝试使用电话号码登录,帐户会自动创建。

如果我随后需要将号码与电子邮件帐户关联,上述方法将返回credential-already-in-use 错误。

Firebase 在其documentation 中推荐以下方法。

var credential = firebase.auth.PhoneAuthProvider.credential(confirmationResult.verificationId, code);

firebase.auth().signInWithCredential(credential);

所以我一起做了这个linkWithCredential 但是,signInWithCredential 返回一个带有result.credential = null的结果

// Sign in user with the account you want to link to
auth.signInWithCredential(credential).then(function(result) {
  console.log("Sign In Success", result);
  var currentUser = result.user;

  return prevUser.linkWithCredential(result.credential) //this line doesn't work.
    .then(function(linkResult) {

      return auth.signInWithCredential(linkResult.credential);
    })
}).catch(function(error) {

  console.log("Sign In Error", error);

});

由于 result.credential = null,我无法继续使用 LinkWithCredential。

我也试过linkWithPhoneNumber,但它返回一个验证ID,我仍然无法合并帐户。

❓请问你们是如何将 phoneNumber 帐户与其他帐户(Facebook/google/email)合并的?

【问题讨论】:

标签: javascript reactjs firebase firebase-authentication


【解决方案1】:

如果您想将现有帐户与电话号码相关联,您可以使用linkWithPhoneNumber。基本上你在用户登录后(比如说用谷歌),你检索用户信息,然后再将该凭据与电话号码链接起来。我找到了一个和你的问题link here类似的详细答案

【讨论】:

  • 我也试过linkWithPhoneNumber,但它返回一个验证ID,我仍然无法合并帐户。它不返回凭据
  • 但无论如何我都会给你赏金。
  • 如果我想反过来怎么办。如果我已经创建了一个带有电话号码的用户,我如何将一个电子邮件地址帐户与其关联。你能帮我解决这个问题吗?
【解决方案2】:

您可以使用身份验证提供程序链接:

async sendSmsVerification() {
// 'recaptcha-container' is the ID of an element in the DOM.
const applicationVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container')
const provider = new firebase.auth.PhoneAuthProvider()

const confirmationResult: firebase.auth.ConfirmationResult = await this.userData.linkWithPhoneNumber('+1nnnnn', applicationVerifier)
.catch(error => error)

const verificationCode = window.prompt('Please enter the verification ' +
  'code that was sent to your mobile device.')

  const res = await confirmationResult.confirm(verificationCode)
  .catch(error => error)
  console.log('res', res)

applicationVerifier.clear()

return res  }

res 会告诉您是否成功、用户输入了错误的代码或电话号码已被使用。 作为奖励,用户现在还可以使用电话号码登录。

您可以在此处找到更多详细信息: https://firebase.google.com/docs/auth/web/account-linking?authuser=0

【讨论】:

  • 如果手机号已经是注册账号(短信登录),这个就不行了
【解决方案3】:

也许是这样的

const confirmationResult = await firebase.auth().currentUser.linkWithPhoneNumber(phoneNumber, recaptcha);
//Prompt your user for the code they recieved via sms here
confirmationResult.confirm(code);

您有一个通过用户名/密码、facebook google 等登录的用户... 可以吗?

因为您没有从电话关联中获得验证 ID,而是获得了 ConfirmationResult

【讨论】:

  • 我也试过linkWithPhoneNumber,但它返回一个验证ID,我仍然无法合并帐户。
猜你喜欢
  • 2021-06-26
  • 1970-01-01
  • 2018-03-21
  • 2019-01-11
  • 2019-11-17
  • 2019-11-21
  • 2021-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多