Firebase: how to check if a user is registered?

New bee for Android Firebase; I am testing Firebase authentication and trying to check if a user is logged in to do this, I use this simple code:

null != mAuth.getCurrentUser(); 

And this returns true , although I have not logged in, but I turned on anonymous login, and I thought it might be a problem, but when I check mAuth.getCurrentUser().isAnonymous() I get false .

Checking mAuth.getCurrentUser() in the IDE shows the following: enter image description here

Any directions here.

+16
source share
4 answers

I had a similar problem, and due to the fact that exiting Firebase is not enough, it will automatically enter automatically.

You must also exit using GoogleSignInApi:

 firebaseAuth.signOut(); Auth.GoogleSignInApi.signOut(apiClient); 
+15
source

I think it works like:

 FirebaseAuth.getInstance().getCurrentUser() 

It should return null if the user is not logged in.

+17
source

To check if a user is logged in:

  private FirebaseAuth firebaseAuth; FirebaseAuth.AuthStateListener mAuthListener; 

Then in your onCreate:

  firebaseAuth = FirebaseAuth.getInstance(); mAuthListener = new FirebaseAuth.AuthStateListener(){ @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){ FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if(user!=null){ Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } } }; 

This can be created in the authorization state class or in the interface. Then you can simply call the authentication state or enable any activity with which you want to do this:

Checking the authorization status when performing an action:

  @Override protected void onStart() { super.onStart(); firebaseAuth.addAuthStateListener(mAuthListener); } @Override protected void onResume() { super.onResume(); firebaseAuth.addAuthStateListener(mAuthListener); } @Override protected void onStop() { super.onStop(); firebaseAuth.removeAuthStateListener(mAuthListener); } 

Now about your login: after setting your logical values ​​for verification, the user has entered the state: (by default, the logical value is set to false)

 firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { progressBar.setVisibility(View.GONE); if (!task.isSuccessful()) { //set a your boolean to false current_user_db.setValue(false); 

and, if the user has successfully logged in, set it to true, and then the intention:

 else { String userid = firebaseAuth.getCurrentUser().getUid(); DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference("Users").child(userid); current_user_db.setValue(true); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } 

After that, your node will be set to False if the user is logged in, or false if the user is not logged in.

+4
source

Kotlin version

if you use Kotlin just do this:

 //properties var firebaseAuth: FirebaseAuth? = null var mAuthListener: FirebaseAuth.AuthStateListener? = null //in onCreate function firebaseAuth = FirebaseAuth.getInstance() mAuthListener = FirebaseAuth.AuthStateListener() { fun onAuthStateChanged(@NonNull firebaseAuth:FirebaseAuth) { val user = FirebaseAuth.getInstance().getCurrentUser() if (user != null) { val intent = Intent( this@LoginActivity , MainActivity::class.java) startActivity(intent) finish() } } } 
0
source

Source: https://habr.com/ru/post/1268896/


All Articles