使用 Firebase for web 我可以成功创建匿名用户。我还可以创建一个新的电子邮件/密码用户。但是当尝试将匿名用户转换为电子邮件/密码用户时,我收到错误:
auth/provider-already-linked
User can only be linked to one identity for the given provider.
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.
});
您不应致电
createUserWithEmailAndPassword
来升级匿名用户。这将注册一个新用户,注销当前登录的匿名用户。
您只需要用户的电子邮件和密码。相反,IDP 提供商(例如 Google、Facebook)将要求完成完整的登录流程,以获得令牌来识别用户。不过,我们建议对这些使用
linkWithPopup
。
示例:
// (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);
});
让我知道这是否有效!
以匿名用户身份登录后,运行此代码以引发弹出窗口并将您的匿名用户与某些 OAUTH 提供程序连接起来
const provider = new firebase.auth.FacebookAuthProvider()
firebase.auth().currentUser.linkWithPopup(provider)
console.log(provider)
对于 iOS、Swift 5 创建
credential
使用
EmailAuthProvider.credential(withEmail: , password: )
示例:
let credential = EmailAuthProvider.credential(withEmail: emailTextField.text!, password: passwordTextField.text!)
Auth.auth().currentUser?.link(with: credential, completion: { (authDataResult: AuthDataResult?, error) in
// ...
})