Support Topics Documentation Slack YouTube Blog

User Session auto relogin does not work

Dear Backendless,

this might be the same topic as posted in: User Session
But I seem to get a fault message with the same code when my session gets expired.

I get the userObjectId from the “getStorage().get()”, so that works fine.
But with the "Backendless.Data.of(BackendlessUser.class).findById(userObjectId, new AsyncCallback() "
I get the fault.message that the session has expired.
So it will never continue to go my “findUsername(BackendlessUser response)”
Is there something else I need to do when I have found the userObjectId?

Yes I have also have stayLoggedIn to “,true);”

Error message:

idleLogin start
idleLogin userObjectLogin E3F3CAD3-3497-82B1-FF6A-E133A3CA6B00

D/ActiveLootFragment: idleLogin fault Session timeout. Url: https://api.backendless.com/9899A2F8-3DC6-5C94-FF20-89FD58136300/7C3E6712-1A87-4E90-A287-47B44C4CEFA8/page/session_expiration/index.html

My code below:

String userObjectId = UserIdStorageFactory.instance().getStorage().get();
Backendless.Data.of(BackendlessUser.class).findById(userObjectId, new AsyncCallback() {
@Override
public void handleResponse(BackendlessUser response) {
//find user
Log.d(TAG,"idleLogin User Object: " + response);
findUsername(response);
}

                @Override
                public void handleFault(BackendlessFault fault) {
                    Log.d(TAG, "idleLogin fault " + fault.getMessage());
                 }
            });
        }

public void findUsername(BackendlessUser response) {
String currentUserId = response.getUserId();
Log.d(TAG,"findUsername found: " + response);
if (currentUserId != null) {
Backendless.UserService.findById(currentUserId, new AsyncCallback() {
@Override
public void handleResponse(BackendlessUser user) {
ContactsApplication_OLD.user = user;
String socialAccount = (String) user.getProperty(“socialAccount”);
Log.d("Social Account: ", "Type: " + socialAccount);
if (socialAccount.equals(“GOOGLE_PLUS”)){
//Go to Google login page
Intent intent = new Intent(getContext(), LoginWithGooglePlusSDKActivity.class);
startActivity(intent);
}
else if (socialAccount.equals(“FACEBOOK”)){
//Go to Facebook login page
Intent intent = new Intent(getContext(), LoginWithFacebookSDKActivity.class);
startActivity(intent);
}
else {
Log.d("findUsername: ", "Social Account was not found: ");
//Go to Choose Social Network activity
Intent intent = new Intent(getContext(), ChooseSocialNetworkActivity.class);
startActivity(intent);
}
}
@Override
public void handleFault(BackendlessFault fault) {
Log.d("Social Account: ", "handleFault: " + fault.getMessage());
}
});
}
}

I am looking forward to your reply!

Best regards,
Robin

Hello @Robin_van_Kuijk

This is not userObjectId, this is userToken.
To get the current user use:

BackendlessUser currentUser = Backendless.UserService.CurrentUser();

Documentation - Get Current User, about userToken.

Hi @Vladimir_Yalovy,

I see what you mean with: BackendlessUser currentUser = Backendless.UserService.CurrentUser();
I can still get the user information with this request, even though session has ended.
The work around I have right now is redirecting the user to the login page and restart the login process,
because of the feedback of the Backendless.UserService.CurrentUser(); I know what social login to choose and login automatically (thanks to your solution)

Is there a quick way to get this user loggedIn again instead of redirecting the app to my login page and (auto) login from there?
To keep the current/last page of the app open?
Like “Backendless.UserService.login(currentUser)” or something?

Thanks in advance!

Hello @Robin_van_Kuijk

Backendless.UserService.login( currentUser.getEmail(), currentUser.getPassword() );
1 Like