0

I use firebase-auth for login:

The method I use to login with firebase is

firebaseAuth .signInWithEmailAndPassword(email, password)

After login the only thing I need from FirebaseAuth is a valid token, the token is sent to backend as a header in other requests. The way I handle the the retrieval of the token is like this(this is also a sample I found on stackoverflow, other people claimed it worked for them):

public class FirebaseUserIdTokenInterceptor implements Interceptor {

    // Custom header for passing ID token in request.
    private static final String X_FIREBASE_ID_TOKEN = "YOUR-CUSTOM-HEADER";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        try {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user == null) {
                throw new Exception("User is not logged in.");
            } else {
                Task<GetTokenResult> task = user.getToken(true);
                GetTokenResult tokenResult = Tasks.await(task);
                String idToken = tokenResult.getToken();

                if (idToken == null) {
                    throw new Exception("idToken is null");
                } else {
                    Request modifiedRequest = request.newBuilder()
                            .addHeader(X_FIREBASE_ID_TOKEN, idToken)
                            .build();
                    return chain.proceed(modifiedRequest);
                }
            }
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}

There are other people on stackoverflow saying this implementation works for them. This implementation works for me as well, I can get a valid token inside this interceptor and I can append it to other requests in the header.

The problem is - after using the application for a couple of days, or after leaving the app in the background for a while, this implementation stops working. More exactly, FirebaseAuth.getInstance().getCurrentUser() starts returning null and I can't get the app working again unless I manually sig out and login again.

I'm aware of some writings on this topic like this one https://medium.com/firebase-developers/why-is-my-currentuser-null-in-firebase-auth-4701791f74f0

and other answers pointing to use onIdTokenChanged(FirebaseAuth auth) in order to get a valid refresh token.

Which is the right way of always having a valid token ?

JUST TO MAKE IT VERY CLEAR:

  1. My login works with no issues
  2. I'm able to get a working token after login
  3. I'm also able to get a working token even for a couple of days, but at some point FirebaseAuth.getInstance().getCurrentUser() starts to return null and it doesn't start working again unless I sign out and login again.

Can you please point me which is the right implementation for keeping a valid token after login with firebase?

It would also be useful if you could provide a minimal working code. Thank you!

Uruos
  • 83
  • 7

0 Answers0