firebase / firebase/firebase-android-sdk

ClientInterceptor and Firebase IdToken

Open
#5,428 6 comments 0 reactions 0 assignees View on GitHub
api: auth type: feature request
Dominant language
Java
Stars
2.6k
Forks
710
Avg merge
2d 23h
Merged PRs (30d)
34

Description

Hi, I am usign GRPC service and 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 `java.lang.IllegalStateException: Must not be called on the main application thread`

```
public class CustomHeaderInterceptor implements ClientInterceptor {
@Override
public ClientCall interceptCall(MethodDescriptor method, CallOptions callOptions, Channel next) {
return new ForwardingClientCall.SimpleForwardingClientCall(next.newCall(method, callOptions)) {
@Override
public void start(Listener 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 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));
}
}
};
}
}
```

I have tried also `CompletableFuture`
```
Task task = user.getIdToken(false);
CompletableFuture completableFuture = new CompletableFuture<>();
task.addOnCompleteListener(new OnCompleteListener<>() {
@Override
public void onComplete(Task task1) {
GetTokenResult tokenResult = task1.getResult();
String idToken = tokenResult.getToken();
completableFuture.complete(idToken);
}
});
String idToken = completableFuture.get();
```
but the code stop on `completableFuture.get()`

How can I do to ask and append on each call the firebase token?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.