Firebase将匿名用户帐户转换为永久帐户错误 [英] Firebase Convert Anonymous User Account to Permanent Account Error

查看:60
本文介绍了Firebase将匿名用户帐户转换为永久帐户错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firebase for Web,我可以成功创建一个匿名用户.我也可以创建一个新的电子邮件/密码用户.但是,当尝试将匿名用户转换为电子邮件/密码用户时,出现错误:

Using Firebase for web I can successfully create an anonymous user. 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在此处将匿名帐户转换为永久帐户"部分下记录了该过程: https://firebase.google.com/docs/auth/web/anonymous-auth

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

这是帐户链接代码.匿名用户已登录.

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

推荐答案

您不应致电createUserWithEmailAndPassword升级匿名用户.这将注册一个新用户,退出当前已登录的匿名用户.

You should not call createUserWithEmailAndPassword to upgrade the anonymous user. This will sign up a new user, signing out the currently signed in anonymous user.

您需要的只是用户的电子邮件和密码.相反,IDP提供商(例如Google,Facebook)将要求完成其完整的登录流程,以获取其令牌来识别用户.我们建议您使用 linkWithPopup linkWithRedirect .

All you need is the email and password of the user. IDP providers (e.g. Google, Facebook), on the contrary, will require to complete their full sign in flow to get their tokens to identify the user. We do recommend to use linkWithPopup or linkWithRedirect for these, though.

示例:

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

让我知道是否可行!

这篇关于Firebase将匿名用户帐户转换为永久帐户错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆