Firebase Authentication with Android AccountManager

I want to create an Android app using Firebase. Firebase provides demo code for the login screen https://github.com/firebase/firebase-login-demo-android .

Rather than having to enter the user type in their credentials, I would like the user to be able to use the account information that the user has already entered into the centralized Android account manager.

I saw the entity docs AccountManager

at https://developer.android.com/reference/android/accounts/AccountManager.html and Firebase android authentication guide at https://www.firebase.com/docs/android/guide/user -auth.html , but I'm too much a noob to figure out how to do it all.

Any advice / pointers / code sample would be appreciated. Or let me know if I am barking the wrong tree.

+3


source to share


1 answer


Fire Engineer is here,

This is a perfectly legal way to do it - we have had several people who have chosen to integrate this in the past.

The short answer is that it takes two steps:

  • Getting the appropriate credentials / tokens from the AccountManager for the desired provider.
  • Authenticate with this identity / token in firebase auth.

This looks like a good resource for the first step (learn about AccountManager). Then, in doCoolAuthenticatedStuff (), you follow our typical auth flow :



Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthToken("google", "<OAuth Token>", new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        // the Google user is now authenticated with your Firebase app
    }
    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        // there was an error
    }
});

      

For Google we use the "email" scope, but each provider will be different.

I have an example of something similar in the iOS world (Twitter login using ACAccountStore which is very similar to Android AccountManager), if the thread helps at all.

It looks like it would be a good recipe / entity, so I'll see what I can do!

+1


source







All Articles