Anthony Anthony - 2 months ago 10
Android Question

How deal with cold start of an app where user has beeing logged with Firebase at previous run

Thank to Firebase the user can logged with the help of G+, Facebook or Twitter. When they are logged, everything is fine.

When the Android app is closed and re-opened, how to reenable the previous succeed logged user with the Firebase API. It is not explained neither in the app demo or in the documentation.

For exemple for Facebook, the sdk seems to save the token, that 's why the button is at connected state (showing that you can disconnect). But what about Firebase and for other authentication systems.

Answer

Thanks to the @Frank-van-Puffelen answer, I had some trials until I get something relevant (at least for me : comment are welcome to improve).

I have based my OAuth architecture into 3 mains components : fdsfds

  1. One single AuthStateListener that is located in the Application.
  2. One Utils Singleton OAuthManager that deal with all authentication process
  3. One or Many Activities that deals with Authentification user interaction (Signin Buttons and so on)

Application Class

FacebookSdk.sdkInitialize(this);

Firebase.setAndroidContext(this);
Firebase.getDefaultConfig().setLogLevel(Logger.Level.DEBUG);
Firebase.getDefaultConfig().setPersistenceEnabled(true);

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.addAuthStateListener(new Firebase.AuthStateListener() {
    @Override
    public void onAuthStateChanged(AuthData authData) {
        if (authData != null) {
            // user is logged in
            // create a partialUser from authData
            OAuthManager.getDefault().setAuthenticatedUser(authData);
            // fetch, merge and save back the partialUser with server registerUser.
            OAuthManager.getDefault().startFetchingUserInfo();
        } else {
            // user is not logged in
            // Try to retrieve the user from Facebook SDK             
            // Try to retrieve the user from "Token and Id save in Android Preferences (in case of issue, or cache reset from Firebase))
            // In retrieve is not possible, clean auth data 
            OAuthManager.getDefault().retrieveOAuth(MilleZimU.getInstance());
        }
    }
});

OAuthManager

Here is every services that deal with authentication (part has been copy from the Firebase dedicated demo activity)

SignInActivity

Here only remain the part that deal with UI interaction.

Retreiving ?

I'm not sure this is necessary, but case to case (maybe due to crash or update of the app), authentication status where different from Firebase|Prefs|FacebookSdk. I will see with time.