FirebaseAuth.getInstance().getCurrentUser() returns a removed user - android

I signed up as new User and FirebaseAuth.getInstance().getCurrentUser() returned correctly the logged in user. Both in onCreate() and onStart() methods.
Then, I removed that user from the Firebase Authentication console. I was expecting to have FirebaseAuth.getInstance().getCurrentUser() = null. But it still returns the user that I have removed. Both in onCreate() and onStart() methods.
How is it possible?

You need to attach a listener to the AuthState, try this:
//Declaration and defination
private FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
//Do anything here which needs to be done if user is null
}
else {
}
}
};
//Init and attach
private FirebaseAuth firebaseAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseAuth = FirebaseAuth.getInstance();
firebaseAuth.addAuthStateListener(authStateListener);
}

Deleting a user in the console does not instantly notify any client apps where that user might be signed in. From the client app's perspective, there is still a user with a user ID and an auth token. But that auth token will no longer be able to work with data in Realtime Database, Cloud Firestore, and Cloud Storage protected by user-based security rules. The token will also no longer validate with the Firebase Admin SDK. These actions you can check for failure. Firebase Auth will eventually try to refresh the token within an hour, which will fail, and the user will be signed out.

Related

Need to relogin if i want to retrieve data

When i login into my app, it works fine and retrieve data from firebase. But when i close the app and open it later the app don't retrieve it. I need to relogin to get that functioning again. Please help.
You need to listen to onAuthStateChanged listener to detect when the user is ready:
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null) {
// User signed in.
} else {
// User signed out.
}
}
};

Sign in with Facebook if account already linked with google during initial signup in Firebase Android

I am trying to sign in with facebook in the reinstallation of an android app. Initially I signed up using Google and successfully linked it with firebase.
But when I try to do with facebook it gives a
FirebaseAuthUserCollisionException
I read in the Firebase Documentation that you can do so by
FirebaseUser prevUser = currentUser;
currentUser = auth.signInWithCredential(credential).await().getUser();
// Merge prevUser and currentUser accounts and data
// ...
but here await() method no longer exists. Also after searching a bit I found out this solution
Tasks.await(mAuth.signInWithCredential(credential)).getUser();
But this also gives an error when getting the current user which is already linked. What can I do to solve this?
There is no need in .await() method to get firebase user.
Use FirebaseAuth.AuthStateListenerinstead.
So you implement firebase sign in with something like this:
FirebaseAuth.getInstance()signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "login success");
} else {
Log.d(TAG, "login error");
}
}
});
And implement AuthStateListener which is trigerred every time user state is changed:
private FirebaseAuth.AuthStateListener authStateListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initAuthStateListener();
}
private void initAuthStateListener() {
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
//user is not logged in
} else {
//user is logged in
//logic to finish the activity and proceed to the app can be put here
}
}
};
}
#Override
protected void onStart() {
super.onStart();
FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}
#Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
}
Note: onAuthStateChanged is trigerred once when authStateListener is added to firebase auth instance.
Also make sure Prevent creation of multiple accounts with the same email address parameter is set on firebase console (Authentication >> SIGN-IN METHOD >> Advanced >> One account per email address (Change) ).

How to identify existing user login Firebase?

How to identify the id (from push) of existing user?
I created a node using push when a user is registered, but when the user try to login again, I don't know how to identify it directly because its a different key.
Is it wrong to use push instead of the authentication id as my node? Because I want to retrieve the data I kept in my db for that user.
// [START auth_state_listener]
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) { //I WANT TO RETRIEVE DB DATA HERE
You ask: Is it wrong to use push instead of the authentication id as my node? The answer is almost certainly, yes. The documentation for FirebaseUser.getUid() states:
Returns a string used to uniquely identify your user in your Firebase
project's user database. Use it when storing information in Firebase
Database or Storage, or even in your own backend
The completion of the code in your post would be something like:
if (user != null) {
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference("users").child(user.getUid());
ref.addListenerForSingleValueEvent(...);
}

Firebase getCurrentUser method explanation required

Can any one tell me that what does get Current User do?
private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
progressDialog = new ProgressDialog(this);
firebaseAuth = FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser() != null){
//start profile activity
finish();
startActivity(new Intent(getApplicationContext(),Profile.class));
}
I am confused on which user get Current User pick?
FirebaseAuth.getInstance().getCurrentUser() returns the currently logged in user in firebase. The user which whith whom the credentials you made the login from your android emulator. If a user isn't signed in, getCurrentUser returns null.
firebaseAuth.getCurrentUser() is used get a FirebaseUser object, which contains information about the signed-in user.
If it returns null the user has not signed in else it will be redirected to Profile.class...(In your above code)

Access user information across activities Firebase Android

In one activity, I am signing in the user, and it moves on to another activity where the user is greeted. I am using the built-in Firebase methods for signing in a user using email/password. I have the UID in Firebase Database and this is linked with a name. I am just manually inputting users in Firebase and will implement a sign up activity later. How do I access the UID of the user in the second activity?
Alternatively to passing the uid around, you can use an auth state listener to detect the user in each activity. From that documentation page:
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
This code would typically go into a base-class that you derive your activities from.
just use
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String UID = user.getUid();
It will automatically get user from previous acivity.
You should either save it in SharedPreferences and retrieve it in the second activity or pass it into the second Activity via the Intent object.
SharedPreferences is one of the simpler ways to store data persistently in the app. Data stored in SharedPreferences can be retrieved and modified from almost anywhere in the app. Data will survive phone restarts, and even an app uninstall, because it can be backed up by the system. Here is the official explanation. And here is a good tutorial.

Categories

Resources