Parse for Android : ParseUser.logOut() doesn't log user out - android

I've been playing around with Parse on Android for a few days, I have a problem with ParseUser.logOut() => it actually doesn't log the current user out.
Here's what I do :
private void onClickOnDisconnect() {
ParseUser.logOut();
currentUser = ParseUser.getCurrentUser(); // should be null but isn't...
invalidateOptionsMenu();
Toast.makeText(getApplicationContext(), "Disconnected...", Toast.LENGTH_LONG).show();
}
After the call to ParseUser.logOut(), ParseUser.getCurrentUser() should return null (says the documentation). Still it returns the previously logged in user.
Even though I restart the app the user is still considered as connected.
I use parse framework v1.3.8 (downloaded a 3 days ago). I don't use Facebook SDK, as I've seen a few questions for that specific case.
Thanks in advance

Somewhere in your app, probably the appDelegate you most likely send this message to the PFUserClass
[PFUser enableAutomaticUser];
in this case the
[PFUser currentUser];
will never return nil;

I had the same issue while incorporating logout functionality with facebook login. The error turned out to quite simple and a basic mistake on my part. I had written the following piece of code in my Application class
ParseUser.enableAutomaticUser();
This creates the user every time you run your application even if you have logged out the last time around. Removing this solved my problem.

As I said in the comments above, I got it to work by :
uninstalling the app
deleting the user on the Parse dashboard
reinstalling the app and signing up again
Then the signout/signin mechanism worked miraculously :|
The only reason I can imagine is that I had previously signed up with this user having Automatic user creation enabled, and it somehow stuck to the user entity in Parse.

Experiencing same bug. Now instead of only checking ParseUser as null, I check username too:
ParseUser currentUser=ParseUser.getCurrentUser()
if(currentUser==null)
{
//user doesn't exist
}
else
{
if(currentUser.getUserName()==null)
{
//user oesn't exist
}
}

Try
ParseUser.getCurrentUser().logOut();

I had the same problem. Actually it was a bug in parse sdk and the problem was solved without changing any of my code, just by installing the latest parse android sdk(1.8.1 at the time of writing).

I had the same issue it was because i has automatic user enabled which causes a new anonymous user to be created, it is also explained here
https://www.parse.com/questions/issues-with-parseuserlogout-on-android

Try to logout the current user object, not the class.
ParseUser.getCurrentUser().logout();
and make sure to delete
ParseUser.enableAutomaticUser();
Edit:
When you logout, the User instance will still available, but with null fields. Parse changes all the user's fields into nulls.
So, if you want to check whether the user is logged in or out, just do the following
if(ParseUser.getCurrentUser().getUsername() == null)
{ // user is out
} else {
// user is in }

Related

Firebase SDK Auth Package Trouble with Automatically Loading Scene Once Authenticated Unity

I am stuck with a functionality of the Firebase SDK (Auth package) regarding the Scenes and it's integration. Here's how they work:
1st: Loading Scene
Here, I just added the FirebaseInit code EXACTLY as suggested by Patrick, which it's only function is to call the next scene (Login/Register) once everything loads correctly.
2nd: Login/Register Scene
Here I do all the Login AND ALSO the register logic. I set up a button that alternates between the two (Activating the respective parent gameObject within the Canvas). Once the user log's in, the 3rd scene comes into play.
3rd: App's Main Screen Scene
Main Screen of the app, where the user can LOGOUT and return to the Login Scene.
Problem
I added the 'LoadSceneWhenUserAuthenticated.cs' in the 2nd Scene, and it works (kind of).
It actually does what it is supposed to. If I log in, quit the game without loging out, and open it again, it does come back directly to the 3rd scene. BUT some things are happening and they aren't supposed to.
First
When I Sign Up a user, I call the method 'CreateUserWithEmailAndPasswordAsync()'. Once it completes, it should activate the login screen and stay there, waiting for the user to fill in the password, but the 'FirebaseAuth.DefaultInstance.StateChanged' comes into play and forces the 3rd screen to be loaded, skipping several other steps that should be taken (email registration for example).
Second
As I mentioned in the end of number 1 above, if I try to log in to an account that does not have it's email verified, it works! (due to the 'LoadSceneWhenUserAuthenticated.cs' which is added in the scene). Code:
var LoginTask = auth.SignInWithEmailAndPasswordAsync(_email, _password);
LoginTask.ContinueWithOnMainThread(task =>
{
if (task.IsCanceled || task.IsFaulted)
{
Firebase.FirebaseException e =
task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
GetErrorMessage((AuthError)e.ErrorCode, warningLoginText);
return;
}
if (task.IsCompleted)
{
User = LoginTask.Result;
if (User.IsEmailVerified == true)
{
UIControllerLogin.instance.MainScreenScene();
}
else
{
warningLoginText.text = Lean.Localization.LeanLocalization.GetTranslationText($"Login/VerifyEmail");
}
I know that it's possible to fix this issue by adding an extra scene just before the login scene (as Patrick does in the youtube video) but it doesn't make any sense in my app. It would actually only harm the UX of it.
Patrick's Video:
https://www.youtube.com/watch?v=52yUcKLMKX0&t=264s
I'm glad my video helped!
My architecture won't work for every game, and I tried to boil it down to the bare minimum to get folks started. You may be able to get the functionality you want by adding an additional check in HandleAuthStateChanged:
private void HandleAuthStateChanged(object sender, EventArgs e)
{
if (_auth.CurrentUser != null && !_auth.CurrentUser.IsAnonymous && _auth.CurrentUser.IsEmailVerified)
{
SceneManager.LoadScene(_sceneToLoad);
}
}
but it does sound like, at this point, you'll want to build out a more robust registration/sign in flow that fits your use case.
If you need more help, I might suggest re-posting on the community mailing list or the subreddit. Those resources may be more better suited to discussing various pros/cons of different architectures or spitballing ideas (and feel free to link to any new posts in a comment so myself or others interested can follow along).

AppAuth Relogin

After some back and forth I finally got this to work but I had to use version 0.2.0 because I followed the google guide presented in the Readme.
Anyway, Im struggling with handling what will happen when the oAuth token times out. Then it needs to open the browser again to log in or is there a background process available for this as it automatically redirects back to the app because the server remembers the user so there is no need for a new username/password input?
Im getting a refresh token like this :
if(mAuthService == null){
mAuthService = new AuthorizationService(context);
}
mAuthState.performActionWithFreshTokens(mAuthService, new AuthState.AuthStateAction() {
#Override public void execute(
String accessToken,
String idToken,
AuthorizationException ex) {
if (ex != null) {
return;
}
// Getting the access token...
}
});
Thats working fine but after the user is idle for some time it wont work. How to handle this properly?
Solution for my problem was this:
I changed to using offline_access for the token in the scope. Depending on the site/service you're login into if they accept it or not. For me it was accepted and will keep the user logged in for a long time and removes the need to re-login.

Facebook Android Unity can't post screenshot

I made a unity project and included facebook in it. Everything worked fine until I used OBB spliter.
Actually, I just wanted to share a screenshot of the game so I did this:
private void CallFBLogin()
{
print ("test");
FB.Login("email,publish_actions", LoginCallback);
StartCoroutine (TakeScreenshot ());
print ("test2");
}
When I push a button, this function is called but the problem is that the process finishes before I'm logged with Facebook. When I'm logged in, I'm already out of this function. What's the best way to log, wait to be logged and then launch the coroutine?
I haven't used the Facebook api yet so I can't give you exact code but based on what I see here you need to launch your coroutine from inside the LoginCallback function.
This way it will automatically execute after the user is logged in.
As TheValar stated, you need to do the following..
private void CallFBLogin()
{
print ("test");
FB.Login("email,publish_actions", MyLoginCallback);
print ("test2");
}
void MyLoginCallback(FBResult result)
{
// Do whatever you need, or nothing. You can check if you are logged in correctly
StartCoroutine (TakeScreenshot ());
}
This will guarantee that the take screenshot gets called after you are logged in Facebook

Android catch app crash

I was wondering if it would be possible to override a method (or something like that), that will be called when the application will crash due to some exception.
I'd like to do this, so the next time the user loggs in, it gives him/her a message that the app has crashed and that the bug will be fixed as soon as possible.
Use Thread and setDefaultUncaughtExceptionHandler().
I found a simple little solution that works perfectly:
When my app is created I do something like this:
// Ceck if the app crashed last time
if(this.getIO().readPrimitiveInternalMemoryBoolean(FileNames.SpaceDroid.CrashReport) == true) {
this.getGameLog().d(classTAG, "App crashed last time");
}
else {
this.getGameLog().d(classTAG, "App didn't crash last time");
}
// Flag the app as chrashed and when the app is exited, then mark it as false.
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, true);
When my app is being closed, then I do something like this:
#Override
public void onDispose() {
super.onDispose();
this.getIO().writePrimitiveInternalMemory(FileNames.SpaceDroid.CrashReport, false);
}
You could use a try catch statement and log info in your logs. Also google play provides a mechanism to notify you about crashes in your apps in production.

Facebook API's method: isSessionValid() always returns true

I am using the official facebook SDK for Android and I am facing some issues regarding session management. Specifically, I don't know how to test whether the session is still valid or not.
First I initialize and authorize a connexion to facebook:
Facebook facebook = new Facebook(context.getString(R.string.facebook_app_id));
facebook.authorize(...); // Works ok
Then, I want to disconnect the user:
facebook.logout(context);
But once I have done that, the following still returns true:
facebook.isSessionValid();
Why does it return true?
Anything I'm doing wrong?
Thanks!
Maybe you encountered an older bug. Is your Facebook SDK up to date?
According to the source, logout calls setAccessToken(null);. And if the token is null, isSessionValid() should return false. Here is it's code for reference:
public boolean isSessionValid() {
return (getAccessToken() != null) &&
((getAccessExpires() == 0) ||
(System.currentTimeMillis() < getAccessExpires()));
}
The getter/setter doesn't do any null-checks either, so the call in logout() should have the desired effect.
Im not sure how you integrated facebook in your app but are you checking the access token from the shared preferences? If you are I would check to see that you are actually setting the new access token. For example, after the log out you would want to write out that new access token (null) so next time it would prompt the user to log in again.

Categories

Resources