简体   繁体   English

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

[英]Firebase Convert Anonymous User Account to Permanent Account Error

Using Firebase for web I can successfully create an anonymous user.使用 Firebase 为 web 我可以成功创建一个匿名用户。 I can also create a new email/password user.我还可以创建一个新的电子邮件/密码用户。 But when trying to convert an anonymous user to a email/password user I get error:但是当尝试将匿名用户转换为电子邮件/密码用户时,出现错误:

auth/provider-already-linked
User can only be linked to one identity for the given provider.

Firebase documents the procedure here under section "Convert an anonymous account to a permanent account" here: https://firebase.google.com/docs/auth/web/anonymous-auth Firebase 在此处的“将匿名帐户转换为永久帐户”部分下记录了此处的过程: https://firebase.google.com/docs/auth/web/anonymous-auth

Here's the account link code.这是帐户链接代码。 Anonymous user is signed in.匿名用户已登录。

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.
});

You should not call createUserWithEmailAndPassword to upgrade the anonymous user.您不应调用createUserWithEmailAndPassword来升级匿名用户。 This will sign up a new user, signing out the currently signed in anonymous user.这将注册一个新用户,注销当前登录的匿名用户。

All you need is the email and password of the user.您所需要的只是用户的电子邮件和密码。 IDP providers (eg Google, Facebook), on the contrary, will require to complete their full sign in flow to get their tokens to identify the user.相反,IDP 提供商(例如 Google、Facebook)将需要完成他们的完整登录流程才能获得他们的令牌来识别用户。 We do recommend to use linkWithPopup or linkWithRedirect for these, though.不过,我们确实建议对这些使用linkWithPopuplinkWithRedirect

Example:示例:

// (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);
});

Let me know if that works!让我知道这是否有效!

After you log in as an Anonymous user, run this code to raise Popup and connect your anon user wit some OAUTH provider在您以匿名用户身份登录后,运行此代码以引发弹出窗口并将您的匿名用户与某些 OAUTH 提供程序连接起来

const provider = new firebase.auth.FacebookAuthProvider()
firebase.auth().currentUser.linkWithPopup(provider)
console.log(provider)

For iOS , Swift 5 to create a credential use对于iOS , Swift 5创建credential使用

EmailAuthProvider.credential(withEmail: , password: )

example:例子:

let credential = EmailAuthProvider.credential(withEmail: emailTextField.text!, password: passwordTextField.text!)
        
Auth.auth().currentUser?.link(with: credential, completion: { (authDataResult: AuthDataResult?, error) in

    // ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM