Unable to logout user on Android

Hi,

I’m experiencing the following problem: when I try to log out a user I’m getting the following fault in the handleFault callback: “code: ‘3023’, message; ‘Unable to logout user due to error: user must be logged in.’” The user seems to be logged in just fine however. I can get it’s userId, email, …

Here is my code:

// log in

Backendless.initApp(this, “…”, “…”, “v1” );

protected Boolean doInBackground(Void… params) {
try{
Backendless.UserService.login(mEmail, mPassword);
Log.i( “Login”, mEmail + " success" );
return true;
} catch (BackendlessException exp){
error = exp.getMessage();
return false;
}
}

// log out

Backendless.UserService.logout( new AsyncCallback<Void>(){
public void handleResponse( Void response ){
// user has been logged out.
}
public void handleFault( BackendlessFault fault ){
// something went wrong and logout failed, to get the error code call fault.getCode()
}
});

I’m not sure if I am doing something wrong, or if this is a bug. Thanks on advance for your help.

With kind regards.

Hi Jens,

Could you please check the following:

  1. do you use the latest build of Backendless SDK for Android/Java (v1.5)
  2. what is the setting in Backendless console at: Users > Login > Multiple Login ?

Regards,
Mark

Hi Mark,

Same problem here. Can you suggest me something.

http://support.backendless.com/public/T30g6potGrIJxfYHhNLk.jpg</img>

User is Logged in.

String userEmail = Backendless.UserService.CurrentUser().getEmail()
userTextView.setText(userEmail );

Text View shown user’s email address.
I’m not using “Multiple Logins”, already switched off in console.
But I don’t know why user is not “logged in” yet and why user cannot logout?
if user is not logged in yet, how can I get CurrentUser().getEmail()?
I’m confusing. Please help me.

Hi N M L,

I just tried the following code in a terminal window app:

    Backendless.initApp( APP_ID, ANDROID_SECRET_KEY, "v1"  );
    BackendlessUser user = Backendless.UserService.login( userName, password );
    Backendless.UserService.logout();
    System.out.println( "user logged gout. " + Backendless.UserService.CurrentUser() );

That worked as expected. The output I get is:

user logged gout. null

I do not have an Android device with me right now, I will check on Android once I am in the office.

Any chance you could try the same with a plain java program too?

Regards,
Mark

I used this code in “Splash Screen”.

Backendless.initApp(this,APP_ID, ANDROID_SECRET_KEY, "v1"  );
...
...

   if(Backendless.UserService.CurrentUser()!=null){
      //go to dashboard
   }else{
      //go to login activity
   }

In dashboard Activity

String userEmail = Backendless.UserService.CurrentUser().getEmail()
userTextView.setText(userEmail );

So, user doesn’t need to login again if he left from application by pressing “back button”. Am I using the correct way? If not, can you bring me to a correct way.
Thanks.

When user logs in a special header is returned by Backendless to the client. The header can be obtained using the following code:

String userToken = HeadersManager.getInstance().getHeader( HeadersManager.HeadersEnum.USER_TOKEN_KEY );

If user token is present in HeadersManager, you do not need to do login again, but if it is not there, user is not logged in. Currently, we do not save user token on the client side anywhere, it means so long that the application is active, user is logged in. What you could do is as soon as user logs in, store user token somewhere. When user returns to the app, you need to put user token value into HeadersManager.

Does it help?

Regards,
Mark

Dear Mark,

Sorry for making you busy again. I was finding about HeadersManager in “Backendless API for Android” which I downloaded last week, but nothing inside about it (maybe I downloaded old one or wrong one which do not include about HeadersManager usage). Is it coding under login -> handleResponse and making another query to get USER_TOKEN_KEY and store it in database?
If there is not heavy load to do, can I request you to provide a sample login/logout project file with HeadersManager or update the old one, so new commers will understand for using HeadersManager and they cannot stuck using with login like me.

Thanks a lot,
NML