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 ---
Because it IS slow and for this reason you should avoid using it. Build your own authentication, like I did and you'll discover how significantly faster it is.
Firebase Authentication is based on two tokens: a refresh token that never expires, and an ID token that expires an hour after it's minted and is auto-refreshed by the SDKs.
Every time a user signs in, the user credentials are sent to the Firebase Authentication backend and exchanged for a Firebase ID token (a JWT) and refresh token. Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens.
Limitations of OTP in firebase: The free plan of firebase has Ten Thousand Verification per month, but if you exceed this limit, you need to pay for that.
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 you can call reload
on profile which will be faster than re-login.
Why you might need to reload the profile?
Let's say the user was logged in and you blocked that account from Firebase Dashboard. In this case, user will still be logged in. You can reload
the profile instead of re-login to revoke user access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With