Users have to login each time they start app even though they are logged in

Hi Backendless team

This is something which seems to have crept in the last month or so, not exactly sure when, but is now a definite problem for some users.

A user logs in to Android app. Everything is fine, but then when they restart the app they are asked to login again. The app used to keep them logged in indefinitely but I am having many users now say they are being asked to login to again each time they start the app.

Weird, because I am not getting this probem on my test phone so not sure where to start looking (I am running Android 9 on development phone)

I am referencing implementation ‘com.backendless:backendless:5.7.0’

I call login user like this
Backendless. UserService .login(email, password, true);

Has anything changed you know of or likely to be the cause of this in other versions of Android or different phones?

Many thanks

Mike

Hi @mike-turner

We will check this issue and let you know as soon as possible.
Can you please also provide the code sample you use to check if user is logged in?
And what is your logic to keep the users logged in? Do you check the loggedInUser value? Maybe something like this:

if (Backendless.UserService.loggedInUser() != null) 
  // go to main page
else
  // user is not logged in, go to login screen

Best Regards,
Maksym

Currently I cannot reproduce this problem, @mike-turner.
I need more details about how do you check if user is logged in to understand why this logic doesn’t work.
Also, do you have the analytics? Maybe there are common things for users with this problem? Maybe Android version or phone brand.

Best Regards,
Maksym

Hi @Maksym_Khobotin

Yes, I think my rather old logic is bad! I have an extra check in for some reason which checks for current user. And then I use another check called isValidLogin() . It used to work and still does on my device, however looking at the docs and the forums it looks like checking for backendless.UserService.CurrentUser is not reliable on app start up.

Existing code:
if(Backendless.UserService.CurrentUser() != null) {
isValidLogin = Backendless.UserService.isValidLogin();
}

So will change this to new style
if (Backendless.UserService.loggedInUser() != null)

Yes, the CurrentUser check is redundant.

CurrentUser - BackendlessUser entity that is assigned after the login call.
loggedInUser - ID of the current logged in user that is stored on device.

You definitely need to check the loggedInUser value.

Best Regards,
Maksym

Hi @Maksym_Khobotin

Could you please just verify my logic if you don’t mind.

If I use
if (Backendless.UserService.loggedInUser() != null) {

Am I correct in thinking that I don’t need to use the isValidLogin() check because they are really doing the same thing?
if (Backendless.UserService.isValidLogin()) {

Or should I be wrapping the loggedInUser check inside isValidLogin, but this seems overkill?

if (Backendless.UserService.isValidLogin()) {
                if (Backendless.UserService.loggedInUser() != null) {

finally I presume I set the current user like this… (can’t find it in the docs…)

String userID = Backendless.UserService.loggedInUser();
Backendless.UserService.setCurrentUser(Backendless.UserService.findById(userID));

Many thanks, Mike

Hi @mike-turner!

I would suggest to keep isValidLogin check. This method verifies that the token for your current logged in user is valid on the server.

So the best way to verify the logged in user is:

  1. Check if there is loggedInUser stored on the device
  2. If so, check if the token for this user is valid

Like this:

if (Backendless.UserService.loggedInUser() != null) {
    if (Backendless.UserService.isValidLogin()) {
        // user is logged in and valid, skip the login screen  
    }
}

The current user is set correctly :slight_smile:

Best Regards,
Maksym

Perfect thanks very much.