Facebook link to Firebase Anonymous Auth without calling the Facebook API

I create anonymous sessions in my Firebase application to save user data before creating my accounts. I saw that Firebase allows you to associate your Facebook account with an anonymous account that sounds very neat, but the caveat of this process seems to be that I have to grab the Facebook token myself, outside the warmth and comfort of the amazing Firebase API, which seems weird not designed considering how much of the input stream Firebase seems to do on behalf of the applications.

Sample code on how to connect an anonymous account from your account linking documents :

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

Naturally, I want to use the Firebase method to get the 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 this, I would lose my anonymous session (and my anonymous user ID, which we want to use to log in to the new Facebook).

Is there anyway to get the Facebook token from the Firebase auth mechanism without user logging in and losing the anonymous session that I am trying to convert to an account accessible on Facebook? (The goal is not to require the Facebook API itself, especially since I will also add Google here)

+4
source share
1 answer

, #linkWithPopup #linkWithRedirect:

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

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

- , :

  • -
  • -
  • ,

:

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);
  });
}
+3

All Articles