6

I am creating anonymous sessions in my Firebase application to save user data before they create their accounts. I saw that Firebase allows linking a Facebook login to an anonymous account which sounds really neat, but a caveat of this process seems to be that I have to grab the Facebook token myself, outside the warmth and comfort of the awesome Firebase API, which seems strangely un-developed given how much of the login flow Firebase seems to do on behalf of apps.

A code sample of how to connect an anonymous account from their account linking docs:

var credential = firebase.auth.FacebookAuthProvider.credential(
    response.authResponse.accessToken);

Naturally, I want to use Firebase's way of getting a token

var provider = new firebase.auth.FacebookAuthProvider();

firebase.auth().signInWithPopup(provider).then(function(result) {
     // result.token or whatever would appear here
});

But if I were to run that, I would lose my anonymous session (and my anonymous user ID, which we want the new Facebook login to use).

Is there anyway to get a Facebook Token out of Firebase's auth mechanism without logging the user in and losing the anonymous session that I'm trying to convert into a Facebook Login-able account? (The goal is to not have to call the Facebook API myself, especially as I'll be adding Google here as well)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kyle Hotchkiss
  • 10,754
  • 20
  • 56
  • 82

1 Answers1

6

I think you're looking to #linkWithPopup or #linkWithRedirect:

var provider = new firebase.auth.FacebookAuthProvider();

user.linkWithPopup(provider).then(function(result) {
   console.log('Party ');
});

If for some reason that doesn't cut it, you could always opt to do yourself:

  • Sign in the user anonymously, and store the user somewhere
  • Sign in the user with the provider and store the token somewhere
  • Delete the provider user and then link the account with the token

Quick and dirty example:

var googleToken;
var anonUser;

firebase.auth().signInAnonymously().then(function(user) {
  anonUser = user;
}).catch(function(error) {
  console.error("Anon sign in failed", error);
});

function signInWithGoogle() {
  var provider = new firebase.auth.GoogleAuthProvider();
  firebase.auth().signInWithPopup(provider).then(function(result) {
    googleToken = result.credential.idToken;
  }).catch(function(error) {
    console.error("Google sign in failed", error);
  })
}

function deleteAndLink() {
  firebase.auth().currentUser.delete().then(function() {
    var credential = 
      firebase.auth.GoogleAuthProvider.credential(googleToken);
    anonUser.link(googleCredential);
  }).then(function() {
    console.log("Link succeeded");
  }).catch(function(error) {
    console.error("Something went wrong", error);
  });
}
fny
  • 31,255
  • 16
  • 96
  • 127
  • Hey @faraz, this was exactly what I was looking for. Did you originally leave a note about the API being undocumented before editing this? (Did you add these API docs based on this ticket? Because if so, awesome, that means a lot to me!) – Kyle Hotchkiss Jul 08 '16 at 15:41
  • Yes I mistakingly did, but I was just being sleep deprived idiot. ;D – fny Jul 08 '16 at 19:00
  • ha! I thought you worked for Firebase and just added the docs was all. – Kyle Hotchkiss Jul 08 '16 at 19:15
  • @KyleHotchkiss: Don't forget to assign the bounty before the rep disappears for ever. ;) – fny Jul 11 '16 at 15:18
  • assigned! I apologize, I thought that stack overflow assigned it to the answer automatically. – Kyle Hotchkiss Jul 11 '16 at 16:07