我正在尝试将一个匿名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的一个函数,所以我对它抛出这个错误的原因感到有点困惑。
任何帮助都非常感谢-谢谢。
发布于 2018-12-19 00:08:09
您使用的是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);
});https://stackoverflow.com/questions/53835000
复制相似问题