我正在尝试将匿名Firebase帐户与提供商帐户相关联,并且我使用的是下面的javascript代码,这些代码是从此处的文档中复制的:https://firebase.google.com/docs/auth/web/account-linking
这是我使用的确切代码:
function mergeAccounts(credential){
console.log("merging guest account with provider account");
var auth = firebase.auth();
// Get reference to the currently signed-in user
var prevUser = auth.currentUser;
// Sign in user with another account
auth.signInAndRetrieveDataWithCredential(credential).then(function(user) {
console.log("Sign In Success", user);
var currentUser = user;
// Merge prevUser and currentUser data stored in Firebase.
// Note: How you handle this is specific to your application
// After data is migrated delete the duplicate user
return user.delete().then(function() {
// Link the OAuth Credential to original account
return prevUser.linkAndRetrieveDataWithCredential(credential);
}).then(function() {
// Sign in with the newly linked credential
return auth.signInAndRetrieveDataWithCredential(credential);
});
}).catch(function(error) {
console.log("Sign In Error", error);
});
}
当我以匿名用户身份登录到Firebase,然后尝试使用我的Google帐户登录时,以上代码成功用google登录了我(第console.log("Sign In Success", user);
行按预期工作,并向我显示了该用户控制台中的详细信息。但是然后我得到一个错误,提示Sign In Error TypeError: user.delete is not a function at 16login.js:212
,它指向具有return user.delete().then(function() {
我使用的文档和代码段似乎暗示delete()
是user
的一个功能,因此我对为什么它会引发此错误感到困惑。
非常感谢您的帮助-谢谢。
答案 0 :(得分:0)
您使用的是signInAndRetrieveDataWithCredential
方法,而不是signInWithCredential
,因此返回值是一个UserCredential
对象。您需要通过回调函数的返回属性的user
属性访问user
:
auth.signInAndRetrieveDataWithCredential(credential).then(function(userCredential) {
const user = userCredential.user; // HERE
console.log("Sign In Success", user);
var currentUser = user;
// Merge prevUser and currentUser data stored in Firebase.
// Note: How you handle this is specific to your application
// After data is migrated delete the duplicate user
return user.delete().then(function() {
// Link the OAuth Credential to original account
return prevUser.linkAndRetrieveDataWithCredential(credential);
}).then(function() {
// Sign in with the newly linked credential
return auth.signInAndRetrieveDataWithCredential(credential);
});
}).catch(function(error) {
console.log("Sign In Error", error);
});