Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase how to authenticate user through native facebook app

The firebase authentication api uses a browser pop up (Firebase.authWithOAuthPopup() in the new api cordova example ) . However, on mobile phones, most people use the native facebook app instead. for For cordova phone apps, authenticating through the fb native app has the advantage of not requiring the user to re-enter facebook username and password.

How can fb native app authentication be achieved with the firebase api?

If firebase does not inherently support fb native app authentication, is it possible to use firebase in conjunction with the cordova facebook plugin, which does appear to support native fb app auth. How could this be done?

like image 480
Jarnal Avatar asked Oct 14 '14 20:10

Jarnal


2 Answers

The authWithOAuthPopup() method does not support the native authentication flow, however, using the Firebase reference's authWithOAuthToken() method you can use the OAuth token that the Cordova Facebook plugin returns to log in to Firebase.

Here's an example:

var dataRef = new Firebase('https://<your-firebase>.firebaseio.com');

facebookConnectPlugin.login(['public_info'], function(status) {
  facebookConnectPlugin.getAccessToken(function(token) {
    // Authenticate with Facebook using an existing OAuth 2.0 access token
    dataRef.authWithOAuthToken("facebook", token, function(error, authData) {
      if (error) {
        console.log('Firebase login failed!', error);
      } else {
        console.log('Authenticated successfully with payload:', authData);
      }
    });
  }, function(error) {
    console.log('Could not get access token', error);
  });
}, function(error) {
  console.log('An error occurred logging the user in', error);
});
like image 94
Chris Raynor Avatar answered Oct 19 '22 03:10

Chris Raynor


Just a note that Firebase 3 has changed slightly. Use :

    var user = {};
    var config = {
        apiKey: "<Your API Key",
        authDomain: "<yourapp>.firebaseapp.com",
        databaseURL: "https://<yourapp>.firebaseio.com",
        storageBucket: "<yourapp>.appspot.com"
    };
    firebase.initializeApp(config);

    // Sign in with Token obtained from facebookConnectPlugin.getAccessToken

    var credential = firebase.auth.FacebookAuthProvider.credential(token);

    firebase.auth().signInWithCredential(credential).then(function(result) {
        // The firebase.User instance:
        user = result;
        console.log('User :'+JSON.stringify(user));
        //Can now user the 'user' here
    }, function(error) {
        // Check error.code and error.message
        // Possible error is auth/account-exists-with-different-credential to fetch the providers ???
        // In case of auth/account-exists-with-different-credential error,
        // you can fetch the providers using this:
        if (error.code === 'auth/account-exists-with-different-credential') {
            firebase.auth().fetchProvidersForEmail(error.email).then(function(providers) {
                // The returned 'providers' is a list of the available providers
                // linked to the email address. Please refer to the guide for a more
                // complete explanation on how to recover from this error.
            });
        }
    });
like image 40
ferdil Avatar answered Oct 19 '22 04:10

ferdil