dshukertjr
dshukertjr

Reputation: 18770

Merge anonymous account to email and password account in firebase with polymer

I am creating a web app with polymer and firebase, and I want be able to merge anonymously logged in accounts to be able to merge to an email and password account.

I found the official documentation on how to achieve this https://firebase.google.com/docs/auth/web/anonymous-auth , but when I try it out, it does not work.

Documentation provides the code sample

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

I am confused at this point since this "auth" variable popped out of no where.

I have tried substituting the user property of my element

<firebase-auth 
  id="auth"
  user="{{user}}"
  on-error="handleError">
</firebase-auth>

, but it gave me the following error:

Uncaught TypeError: this.user.link is not a function

What variable am I supposed to have for the "auth" in this example? Or, how can I merge anonymous account to an email and password account using polymerfire?

Upvotes: 2

Views: 781

Answers (2)

dshukertjr
dshukertjr

Reputation: 18770

Okay, I figured it out.

Turns out that the "link" method is deprecated and instead, we have to use "linkWithCredential", so the code would be

this.$.auth.auth.currentUser.linkWithCredential(credential).then(function(user) {
  console.log("Anonymous account successfully upgraded", user);
}, function(error) {
  console.log("Error upgrading anonymous account", error);
});

Upvotes: 3

sfeast
sfeast

Reputation: 966

Polymer's firebase-auth element has an auth property. So you could bind to it:

<firebase-auth 
  id="auth"
  user="{{user}}"
  auth="{{auth}}"
  on-error="handleError">
</firebase-auth>

& then use this.auth directly in your containing element or you could just reference it directly in your code w/out binding like:

this.$.auth.auth

Side note - makes it kind of confusing that the docs for firebase-auth give an example element id of "auth" leading to the "auth.auth" above.

Upvotes: 1

Related Questions