Solution 1 :
You can use your second bit of code inside the first bit, after the sign-in completes.
// ***I want to get the token here***
FirebaseUser fbUser = task.getResult().getUser();
fbUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
// ...
}
Problem :
I’m using Firebase auth to login users to my webapp and android app.
The flow for the webapp lets me log the user in from the client, then pass the Firebase token to my server where I verify with Firebase before adding various user info to my database.
I’m now trying the same thing for Android, but am having some problems getting the token.
Here is the login code:
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// ...
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener((MainActivity) context, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// ***I want to get the token here***
FirebaseUser fbUser = task.getResult().getUser();
// send user details and token to server
} else {
// If sign in fails, display a message to the user.
}
}
}
});
Elsewhere, I see that in the Firebase docs, there is this snippet that gets the Task<GetTokenResult>
, which is what I want, but I can’t seem to get this in the createUserWithEmailAndPassword
method.
From the firebase docs
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String idToken = task.getResult().getToken();
// Send token to your backend via HTTPS
// ...
} else {
// Handle error -> task.getException();
}
}
});
How can I get the token in createUserWithEmailAndPassword
?