Adrian Muljadi
Adrian Muljadi

Reputation: 168

FirebaseUI Auth offline

I'm going through my first course in Firebase (Udacity), and have code that looks like this implementing Firebase Auth

private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
....
    mAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                onSignedInInitialize(user.getDisplayName());
            } else {
                // User is signed out
                onSignedOutCleanup();
                startActivityForResult(
                        AuthUI.getInstance()
                                .createSignInIntentBuilder()
                                .setIsSmartLockEnabled(false)
                                .setProviders(
                                        AuthUI.EMAIL_PROVIDER,
                                        AuthUI.GOOGLE_PROVIDER)
                                .build(),
                        RC_SIGN_IN);
            }
        }
    };

@Override
protected void onResume() {
    super.onResume();
    mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}

This code runs into an infinite loop when the app is started offline.

onResume -> onAuthStateChanged-> startActivityForResult-> onActivityResult (Fails with ErrorCodes.NO_NETWORK) -> onResume

Is there a way to get to FirebaseUI-Auth login screen offline, so that I can login using Android Smart Lock. Or at least, prevent the infinite loop as above?

Please let me know if you need extra details

Upvotes: 1

Views: 1716

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599166

Authenticating the user requires an active connection. There is no way for Firebase to authenticate your users without connecting to its servers.

So one way to handle this is to only show the login dialog when the user's device is connected to the network and/or connected to Firebase.

Upvotes: 3

Related Questions