0

can someone help me how to convert an anonymous account (signInAnonymouslyAndRetrieveData) to a permanent account ? I have tried this:

firebase.auth().currentUser.linkAndRetrieveDataWithCredential(credential).then(function(usercred) {
  var user = usercred.user;
  console.log("Anonymous account successfully upgraded", user);
}, function(error) {
  console.log("Error upgrading anonymous account", error);
});

but i'm getting an

cannot read property "linkAndRetrieveDataWithCredential" of null

error.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
nmsdvid
  • 2,832
  • 2
  • 21
  • 21

1 Answers1

1

firebase.auth().currentUser will be null if there's no currently signed in user.

Ensure your anonymous user is still signed in then as in your example above you can then upgrade your user using linkAndRetrieveDataWithCredential

const credential = firebase.auth.EmailAuthProvider.credential(email, password);
const currentUser = firebase.auth().currentUser;

if (currentUser) {
  currentUser.linkAndRetrieveDataWithCredential(credential).then((userCredential) => {
    const user = userCredential.user;
    console.log("Account linking success", user);
  }, (error) => {
    console.log("Account linking error", error);
  });
}

References:

Salakar
  • 6,016
  • 2
  • 18
  • 24
  • What do you mean by "Ensure your anonymous user is still signed in"? I have an anonymous user created just by opening the app, and they have a UID. But they are not signed in besides that. When I try to use linkAndRetrieveDataWithCredential per the Firebase instructions to link a newly-registered user with the data they generated when they were an anonymous user, I get the error "User can only be linked to one identity for the given provider." – Greg Aug 28 '18 at 06:48