Skip to content

ClientInterceptor and Firebase IdToken  #2

Description

@developerGM

Hi, I am rad your article and I think that is really interesting.
But I would like to know how can I solve a problem...I need to ask to sdk Firebase the token

Hi, I am trying to extends ClientInterceptor to append some headers and ask to Firebase to give me a IdToken but I don't understand how can do it because task.getResult() go to in exception java.lang.IllegalStateException: Task is not yet complete

I have tried also with a sync mode
GetTokenResult tokenResult = Tasks.await(task, 20, TimeUnit.SECONDS);
but nothing, exception main thread

public class CustomHeaderInterceptor implements ClientInterceptor {
    @Override
    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
        return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
                headers.put(Metadata.Key.of("X-XXX", Metadata.ASCII_STRING_MARSHALLER), "test_test");

                try {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    if (user == null) {
                        throw new Exception("User is not logged in.");
                    } else {
                        Task<GetTokenResult> task = user.getIdToken(false);
                        GetTokenResult tokenResult = task.getResult();

                        String idToken = tokenResult.getToken();
                        headers.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer " + idToken);

                        super.start(responseListener, headers);
                    }
                } catch (Exception e) {
                    Log.d(this.getClass().getName(), new Gson().toJson(e));
                }
            }
        };
    }
}

How can you solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions