Logout function not clearing ParseUser currentUser after parse migration in Android - 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...

Related

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;

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)

Firebase isEmailVerified returns false even after verifying email

I send email verification and click the link. Then I call isEmailVerified() on FirebaseUser object. This always return null, what may be the reason?
You should refresh the user object after verification as follows :
usertask = mAuth.getCurrentUser().reload();
usertask.addOnSuccessListener(new OnSuccessListener() {
#override
public void onSuccess(Void aVoid) {
user = mAuth.getCurrentUser();
boolean useremailveri = user.isEmailVerified();
String useremailuid = user.getUid();
}
});
First you have to call this method : signInWithEmailAndPassword , at least 1 time after user has verified their email ....
Only after that, you will get isEmailVerified() as true
Change isEmailVerified() to emailVerified;
Re-authenticates the user who updates if the email has been verified ...
https://firebase.google.com/docs/auth/android/manage-users#re-authenticate_a_user
Same technique as #yehyatt´s solution but easier. Sign the user in with firebase, refresh the user and then check if the user is verified.
Sign the user In
Auth.auth().signIn(withEmail: YourEMail, password: YourPassword) { result, error in
if error != nil {
logInErrorReason = "\(error!.localizedDescription)"
// An error ocurred
}else {
// Handle successful login.
}
}
Refresh current user:
Auth.auth().currentUser?.reload(completion: ((Error?) -> Void)?)
Check if he is signed in:
if Auth.auth().currentUser?.isEmailVerified == true { // Handle successful verification}
I think you need to first login with user's Email and Password using signInWithEmailAndPassword then try calling isEmailVerified()
method on returning user.
isEmailVerified should provide correct response, however you might be using it incorrectly.
If user verified his email ( he/she received an email from firebase, and clicked on "verify email" link ) then it will return true;
If user didn't verified his email - it will return false
So solution for this issue is to update you registration process.
When you create new user you should also issue a command to start email verification process await user.sendEmailVerification();
once your user will receive an email and will verify it. your method : user.isEmailVerified will return true
Your registration process should look something like that:
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email,password: password);
FirebaseUser user = result.user;
await user.sendEmailVerification();
Create new user using createUserWithEmailAndPassword
Let it send verification link to your gmail using sendEmailVerification
go to your own gmail, you will see verification link and just click
on that link
try to login with username and email
You will see your own account had already verified and can proceed next setps
https://github.com/FirebaseExtended/flutterfire/issues/2208

Unable to Signup as Parseuser, after failed signup call

I am using Parse.com for my android app.
I have enabled ParseAnonymousUser
I am getting the following error, when test with the following scenario
---> Turn off wifi, and try to signup
---> Then Turn wifi on, without killing the app, and then try to signup again.
I get the the error: "java.lang.IllegalArgumentException: Cannot sign up a user that is already signing up." (error code: -1)
---> If i kill the app, then i successfully signup
Here is my signup code snippet:
ParseUser user = ParseUser.getCurrentUser();
user.setUsername(phoneNum);
user.setPassword(phoneNum);
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.show();
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (getActivity() == null) {
return;
}
mProgressDialog.dismiss();
handleSignupResponse(e, phoneNum, name);
}
});
Please tell me a solution asap. I have been struck here for a day.
You have a mistake in declaring the ParseUser object.
Your mistake:
ParseUser user = ParseUser.getCurrentUser();
The user is not existed yet, and you are requesting it, that doesn't make any sense.
It should be:
ParseUser user = new ParseUser();
And then your rest of the code.

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