Skip to content Skip to sidebar Skip to footer

Get Uid After Signing In Firebase Android

In my app a User can sign up and than sign in, after successfully signing in he's taken to an activity with events and stuff according to his preferences. I am able to sign up and

Solution 1:

AuthStateListener is used to observe changes to the user's sign-in state. So you should have it in your LoginActivity

mAuthListener = newFirebaseAuth.AuthStateListener() {
    @OverridepublicvoidonAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        if (firebaseAuth.getCurrentUser() != null) {
            // intent call to second activity
        }
    }
}

You can bind you Listener to Auth in onStart() method like below

@OverrideprotectedvoidonStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

For getting the current logged in user details you can use

FirebaseAuth.getInstance().getCurrentUser().getUid();

this will return uid as a String.

Solution 2:

Instead of declaring

FirebaseAuthauth= FirebaseAuth.getInstance();
FirebaseUseruser= auth.getCurrentUser();

Add this into your OnCreate

FirebaseUsermCurrentUser= FirebaseAuth.getInstance().getCurrentUser();
    if (mCurrentUser != null) {
        uid = mCurrentUser.getUid();

Then if you want you can declare your uid as public static String uid so that you would not have to rewrite the code and could just use the variable

uid

Post a Comment for "Get Uid After Signing In Firebase Android"