Firebase将匿名用户帐户转换为永久帐户错误

Ayd*_*hew 12 firebase firebase-authentication

使用Firebase for web我可以成功创建匿名用户.我还可以创建一个新的电子邮件/密码用户.但是当尝试将匿名用户转换为电子邮件/密码用户时,我收到错误:

auth/provider-already-linked
User can only be linked to one identity for the given provider.
Run Code Online (Sandbox Code Playgroud)

Firebase会在此处的"将匿名帐户转换为永久帐户"部分中记录此过程:https: //firebase.google.com/docs/auth/web/anonymous-auth

这是帐户链接代码.匿名用户已登录.

return firebase.auth().createUserWithEmailAndPassword(email, password).then(newUser => {

    // Credential is being successfully retrieved. Note "any" workaround until typescript updated.
    let credential = (<any>firebase.auth.EmailAuthProvider).credential(email, password);

    firebase.auth().currentUser.link(credential)
        .then(user => { return user; })
        .catch(err => console.log(err)); // Returns auth/provider-already-linked error.
});
Run Code Online (Sandbox Code Playgroud)

TMS*_*SCH 23

您不应该致电createUserWithEmailAndPassword升级匿名用户.这将注册一个新用户,注销当前已登录的匿名用户.

您只需要用户的电子邮件和密码即可.相反,IDP提供商(例如谷歌,Facebook)需要完成他们的完整登录流程才能让他们的代币识别用户.不过,我们建议使用linkWithPopup或者linkWithRedirect使用这些.

例:

// (Anonymous user is signed in at that point.)

// 1. Create the email and password credential, to upgrade the
// anonymous user.
var credential = firebase.auth.EmailAuthProvider.credential(email, password);

// 2. Links the credential to the currently signed in user
// (the anonymous user).
firebase.auth().currentUser.linkWithCredential(credential).then(function(user) {
  console.log("Anonymous account successfully upgraded", user);
}, function(error) {
  console.log("Error upgrading anonymous account", error);
});
Run Code Online (Sandbox Code Playgroud)

如果有效,请告诉我!

  • 感谢您的回答 - 使用Firebase的客户端移动应用程序也适用相同的逻辑.我在iOS应用程序中遇到了同样的问题,这个逻辑修复了它.关于这一点的Firebase文档不是很清楚 - 他们应该强调这一点(如果谷歌的任何人都读到这一点). (3认同)
  • firebase.User.prototype.link已弃用.请改用firebase.User.prototype.linkWithCredential. (2认同)
  • 文档根本没有很好地解释 linkWithCredential 基本上是 createUserWithEmailAndPassword 的替代品。 (2认同)