[英]Firebase Convert Anonymous User Account to Permanent Account Error
使用 Firebase 為 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
或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);
});
讓我知道這是否有效!
在您以匿名用戶身份登錄后,運行此代碼以引發彈出窗口並將您的匿名用戶與某些 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
// ...
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.