UserService.CurrentUser() is null even if UserTokenStorageFactory retruns token

I try to skip login step like this:

String userToken = UserTokenStorageFactory.instance().getStorage().get();
if (userToken == null || userToken.equals(""))
    showLoginActivity();
else
{
    BackendlessUser user = Backendless.UserService.CurrentUser();
    ... some code here ...
}

But UserService.CurrentUser() is null even if UserTokenStorageFactory returns not empty token.
How to retreive/update current BackendlessUser without calling UserService.login()?
May be I can automatically relogin by existing token or something like this?

Hi,

The presence of userToken in UserTokenStorageFactory doesn’t mean that the CurrentUser is loaded. But when it is present, it is guaranteed that objectId of the logged in user is also present. So you can load it calling

String currentUserId = Backendless.UserService.loggedInUser();

and then retrieve it from PersistenceService and set CurrentUser:

Backendless.UserService.findById( currentUserId, new AsyncCallback<BackendlessUser>()
{
@Override
public void handleResponse( BackendlessUser currentUser )
{
Backendless.UserService.setCurrentUser( currentUser );
....
}
....
} );

You can see the same behavior in our Code Generation UserService sample (in LoginActivity).

1 Like

Thank you, this is exactly what I need :slight_smile:

There does not appear to be an equivalent of Backendless.UserService.loggedInUser() on iOS. How would I replicate this functionality on that platform (get ID of logged in user from prior session)?

Hi Tim,

I am looking at the implementation of the client-side User Service proxy. It looks like all the information is there, but there is no simple accessor. I will open an internal ticket to add that method.

Regards,
Mark

Hi Mark,

Thanks for your reply. Please reply when the updated SDK is available.

Thanks

Hi Tim,

In iOS you have your persistent user in backendless.userService.currentUser property just after your app have been loaded, and you can simply use it, for example, in viewDidLoad method:












- (void)viewDidLoad

{

    [super viewDidLoad];

    

    @try {

        

        NSLog(@"viewDidLoad -> currentUser: %@", backendless.userService.currentUser);

        

        if (backendless.userService.currentUser) {

            

            id user = [backendless.data findById:@"BackendlessUser" sid:backendless.userService.currentUser.objectId];

            NSLog(@"viewDidLoad -> (A) findById: %@", user);

        }

    

#endif

    }

    @catch (Fault *fault) {

        [self showAlert:fault.message];

    }

} 

The fix is made.
Now if setStayLogged = YES the property backendless.userService.currentUser saves ALL user properties which they loaded and changed when user logged.

The demo app UserService from iOS-Samples github demonstrates this feature.
Here is the fixed lib: https://github.com/Backendless/ios-SDK/blob/master/SDK/lib/backendless/backendless.a

I understand the getter… But I have a follow up question. How do you set the token?

I can not find it within the backendless documentation nor the android developer site.

You should not (and probably can not) set a user token. Is it automatically set after successful login call.

I understand. The question becomes…What is the set() method for then? (See attached photo)http://support.backendless.com/public/attachments/f6f2cd18794f0c1e9778ef92370018a5.PNG</img>

Well, this must be the method I was talking about - the one to set the token automatically after the login. As I said, the developers are not supposed to use it directly, that’s why it’s not covered in the documentation.

I guess I must find another method to do this then…Thank you for your help, much appreciated.

What is “this” you are trying to do?

When the user log in, the user needs to stay logged in (The login page will be skipped) if the user chose the option to do so.

It’s already built-in, just set the ‘stayLoggedIn’ parameter to ‘true’ when calling ‘login’: https://backendless.com/docs/android/doc.html#validating-user-login

Thank you for the help. Will use this method.

Thank you for the help. It works.