Search code examples
javascriptfirebasefirebase-authenticationfacebook-authentication

Link Facebook to Firebase Anonymous Auth without calling Facebook API


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)


Solution

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