Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase authentication using Android AccountManager

I'd like to build an Android app using Firebase. Firebase provides demo code for a login screen https://github.com/firebase/firebase-login-demo-android .

But rather than making the user type in their account info, I'd like the user to be able to use the account(s) info that the user already entered in Android's centralized account manager.

I've seen the docs of the AccountManager object at https://developer.android.com/reference/android/accounts/AccountManager.html and the Firebase android authentication guide at https://www.firebase.com/docs/android/guide/user-auth.html, but I'm too much of a noob to grok how to put it all together.

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

like image 384
ronen Avatar asked Sep 27 '22 12:09

ronen


1 Answers

Firebase engineer here,

This is a totally legitimate way of doing this--we've had a number of people choose to integrate this in the past.

The short answer is that it requires two steps:

  • Getting the appropriate credential/token from the AccountManager for the desired provider
  • Authenticating with that credential/token to Firebase auth.

Looks like this is a good resource for the first step (learning about AccountManager). Then in the doCoolAuthenticatedStuff() you would 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 'email' scope, but each provider will be different.

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

This sounds like it would make a good recipe/gist though, so I'll see what I can do!

like image 140
Mike McDonald Avatar answered Oct 05 '22 08:10

Mike McDonald