Home » Android » android – How to speed up the Firebase authentication process

android – How to speed up the Firebase authentication process

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have an Android app with Firebase-backed storage. For authentication, I’m using a Google sign-in, followed by Firebase’s signInWithCredential().

This is sometimes reasonably fast, some other times very slow (2 seconds for the Google sign-in, 6 seconds for Firebase, sometimes longer). This is not acceptable from a UX point of view, especially since this happens every single time the user starts the app.

Is there anything I can do to speed things up?

In a nutshell, with all async handling removed:

result = Auth.GoogleSignInApi.silentSignIn(googleApiClient).get();
// --- That can take over two seconds ---

GoogleSignInAccount acct = result.getSignInAccount();
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
authTask = auth.signInWithCredential(credential);
// --- This can take over six seconds ---
How to&Answers:

Authentication time depends on Network speed and Firebase Servers.

There is no need to login the user every time they launch your application because

FirebaseAuth.getInstance().getCurrentUser()

will not be null unless you explicitly sign out the user with

FirebaseAuth.getInstance().signOut()

In case you want to update the Auth instance for newer changes let’s say the user was logged in and then due to some reasons you blocked that account. In this case, user will still be logged in and use your services. To avoid this you can call reload on profile which will be faster than re-login.