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?
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);
});
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.
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With