Firebase Convert Anonymous User Account to Permanent Account Error

前端 未结 2 1682
后悔当初
后悔当初 2021-02-18 19:22

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

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-18 19:36

    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.

    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.

    Example:

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

    Let me know if that works!

提交回复
热议问题