Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] How to get Firebase auth token in Android login

Posted on November 11, 2022 By

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?

READ  [FIXED] android - Persist states after viewModelLifecyle till the app get closed
Powered by Inline Related Posts
Android Tags:android, firebase, firebase-authentication

Post navigation

Previous Post: [FIXED] java – Access Binding Adapters in multi module project
Next Post: [FIXED] java – Changing value in on item also changes in another item – RecyclerView Android

Related Posts

[FIXED] visual studio – Activity design is breaking while opening input keyboard in Xamarin Android Android
[FIXED] App does not open on Android 6.0.1. But works on emulator and Android 10 Android
[FIXED] android – How to set Context in AlertDialog.Builder() when called in an Adapter in Kotlin? Android
[FIXED] java – Android: mediaplayer inside JsonObjectRequest Android
[FIXED] java – Uninterrupted background music across all activities in Android Studio game Android
[FIXED] android – Data is inserted but wont retrieve using Room Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme