2014-10-14 26 views
8

L'API di autenticazione firebase utilizza un popup del browser (Firebase.authWithOAuthPopup() nella nuova API cordova example). Tuttavia, sui telefoni cellulari, la maggior parte delle persone usa invece l'app nativa per Facebook. Per le app per telefoni cordova, l'autenticazione tramite l'app nativa fb ha il vantaggio di non richiedere all'utente di immettere nuovamente nome utente e password di Facebook.Firebase come autenticare l'utente tramite l'app nativa di Facebook

Come si può ottenere l'autenticazione di app nativa con l'API di Firebase?

Se firebase non supporta in modo intrinseco l'autenticazione app nativa fb, è possibile utilizzare firebase insieme allo cordova facebook plugin, che sembra supportare l'autenticazione nativa per le app fb. Come si può fare?

risposta

22

Il metodo authWithOAuthPopup() non supporta il flusso di autenticazione nativa, tuttavia, utilizzando il metodo del riferimento Firebase authWithOAuthToken() è possibile utilizzare il token OAuth che il plugin Cordova Facebook torna per accedere a Firebase.

Ecco un esempio:

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

Grazie per la risposta e per aver fornito il codice di esempio! – Jarnal

2

Solo una nota che Firebase 3 è leggermente cambiata. Uso:

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. 
      }); 
     } 
    }); 
Problemi correlati