Firebase getCurrentUser method explanation required - android

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)

Related

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

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.

why I get wrong current user in firebase android xamarin

I use this code to define Current user and email in Firebase:
var CurrentUserEmail = FirebaseAuth.Instance.CurrentUser.Email;
It doesn't give the right user and email. Did you have the same issue?
I am not familiar with xamarin, but if you want to get a the current user or his/her email then you should make sure that the user is signed in or else you can't get these details, something like that:
//get the current user like that
var currentuser =FirebaseAuth.Instance.CurrentUser;
//check is user not null (in other words if user signed in)
if(currentuser != null){
//this means user is signed in
//get user uid
var uid= currentuser.Uid;
//get user email
var email= currnetuser.Email;
}
I found my mistake, in case who will meet the same case. I typed StartActivity right away after SignInWithEmailAndPassword:
auth.SignInWithEmailAndPassword(email,password)
.AddOnCompleteListener(this,this);
StartActivity(intent); // incorrect
Right way to do it :
auth.SignInWithEmailAndPassword(email,password)
.AddOnCompleteListener(this,this);
...
public void OnComplete(Task task)
{
if(task.IsSuccessful){
StartActivity(intent);
Finish();
}
}
first you must declare private FirebaseAuth _auth; as a property of your activity.
Then you can access your currentUser this way :
FirebaseUser currentUser = _auth.CurrentUser;

Logout function not clearing ParseUser currentUser after parse migration in Android

I am using Parse SDK 1.13.1 for Android and I need to logout user on button click. it's working but when I call ParseUser.getCurrentUser() it's give me last logged in user instead of null.
For Parse Initialize i write below code :
Parse.initialize(new Parse.Configuration.Builder(this).applicationId("PARSE_APPLICATIONID").clientKey("PARSE_CLIENT_KEY").server("PARSE_SERVER_URL").build());
For logout i write below code
ParseUser.logOut();
and also tried
ParseUser.getCurrentUser().logOut();
ParseUser currentUser = ParseUser.getCurrentUser(); // this should be null but not
But, it seems that ParseUser.getCurrentUser(); after log out is not null.
How I can remove current user from cache?
Please help me to resolve this. Thanks
Whenever you use any signup or login methods, the user is cached on disk. You can treat this cache as a session, and automatically assume the user is logged in.
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// do stuff with the user
} else {
// show the signup or login screen
}
You can clear the current user by logging them out.
ParseUser.logOut();
ParseUser currentUser = ParseUser.getCurrentUser(); // this will now be null
Make sure that you have all your functions correctly set and that you not authenticating the user again etc.
If you have Automatic user creation enabled, logging out and then fetching the current user will produce a new anonymous user. This on by default in the starter projects so that you can start using users and ACLs immediately.
if [PFUser enableAutomaticUser]; set in your appDelegate, current user will never return nill
Please check below:
public static Task<Void> logOutInBackground()
or
public static void logOutInBackground(LogOutCallback callback)
method with ParseUser Like below:
ParseUser.logOutInBackground(); or
ParseUser.logOutInBackground(new LogOutCallback )
Hope it will work...

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.

Parse User log out does not work

I am using Parse 1.9.0 for Android and I need to log out user on button click, so I call:
ParseUser.logOut();
After that I need to open login screen and click on Facebook login button, and to check if user have saved custom custom field.
But, it seems that ParseUser.getCurrentUser() after log out is not null.
How I can remove current user from cache?
Try this:
ParseUser.logOut();
ParseUser currentUser = ParseUser.getCurrentUser(); // this will now be null
The logout() method is called from the current user object itself and to obtain it you call ParseUser.getCurrentUser() and then logout like below:
ParseUser.getCurrentUser().logOut();
now the current user is null.
if ParseUser.enableAutomaticUser(); isn't called anywhere the current user will only be logged in using :
ParseUser.logInInBackground(username, password);
ParseUser parseUser = /*your user data here*/;
parseUser.signUpInBackground();
Don't know what you want to do! But you can check at the onCreate method if the Current User is an Anonymous user and thus prevent it to auto-load Anonymous userData from the cache:
if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
//This is a Logged out Anonymous user
} else {
//Getting Current User who is not logged off
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
//Do your staff
}
}

Categories

Resources