user7696842
user7696842

Reputation:

Android-How to get User info from Firebase after logging in

With my current login activity, I can log in just fine, however I need to get Uid throughout my project.

This is my current login activity:

   //authenticate user
        firebaseAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        progressbar.setVisibility(View.GONE);
                        if (!task.isSuccessful()) {
                            // there was an error
                            if (password.length() < 6) {
                                lPass.setError(getString(R.string.minimum_password));
                            } else {
                                Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Intent intent = new Intent(LoginActivity.this, MapsActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
    }
});

How do I get the user Uid from the login and have it be accessible to all my other activities? Thanks

Upvotes: 1

Views: 15000

Answers (4)

Alex Mamo
Alex Mamo

Reputation: 138824

You can get user info from the FirebaseUser object. In order to achieve this, please use the following code:

FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if (firebaseUser != null) {
            String userId = firebaseUser.getUid();
            String userEmail = firebaseUser.getEmail();
        } 
    }
};

Upvotes: 5

nauman mir
nauman mir

Reputation: 154

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

This line will give you the user ID.

Upvotes: 2

Magesh Pandian
Magesh Pandian

Reputation: 9369

Using FirebaseAuth you can get user uid,

  FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
Log.d(TAG," UserId : "+firebaseUser.getUid()+" , DisplayName"+firebaseUser.getDisplayName());

Upvotes: 0

Chintan Joshi
Chintan Joshi

Reputation: 1307

This is a way to get all details available for user logged in from firebase.

FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
// User Name
mFirebaseUser.getDisplayName();
// User ID
mFirebaseUser.getUid();
// Email-ID
mFirebaseUser.getEmail();
// User-Profile (if available)
mFirebaseUser.getPhotoUrl();

Upvotes: 1

Related Questions