User login session doesn't persist across page reload

Hi,
I’m trying to use the user authentication service with javascript.

I use Backendless.UserService.login( login, password ) to login the users. It works fine and logs the user in.

I then use Backendless.UserService.getCurrentUser() to check if user is logged in. That also works at first.

The problem is when I refresh the page. Backendless.UserService.getCurrentUser() returns null.

Does the user login not persist across page reloads? Or is it a setting somewhere? Or is it not possible?

Thanks!

Hi Andrew,

Currently, the SDK does not persist the logged in state. We’re adding that functionality to the library. Until then you can do the following:

  1. As soon as user successfully logs in, obtain the following value:
    var userToken = Backendless.UserService. getCurrentUser()[“user-token”]

and save it in local storage.

  1. Once the page is reloaded, check if user token had been persisted and if so, place the value into the same object:
    Backendless.UserService.currentUser = {};
    Backendless.UserService.currentUser[ “user-token” ] = savedUserTokenValue;

At this point the login is not required and all API requests will be issued under the user’s identity.

Regards,
Mark

Is there any update on this?
I’m new to Backendless and I’m not able to set the currentUser after the login.

Before my login, I’m doing a “window.location.href=“index”;” but “Backendless.UserService.getCurrentUser()” and “Backendless.Cache.get(“current-user”)” always return null

Hi Tiago,

Based on the reference to window.location, I assume you’re working with JS. Please confirm.

Regards,
Mark

Hi Mark!

Yes, you are correct to assume the JS type.

Best regards,

Tiago

Ok, not sure if I’m on the right track but now I’m able to do so by using “Backendless.UserService.loggedInUser()” as token, that’s right?

I’ve found no “user-token” inside the getCurrentUser object

Regards,

Tiago Schmidt

Are you trying to get the user object on the initial login or after page is reloaded? (assuming you set the “stayLoggedIn” argument to true in the login() call).

Regards,
Mark

I’m trying to get the user object after reload. If null or undefined, I’m redirecting to login page.

Login

Backendless.UserService.login( email, password, true )
var userToken = Backendless.UserService.loggedInUser();
window.location.href="index";

On index page:

var currentUser = Backendless.UserService.loggedInUser();
if (currentUser === undefined) {window.location.href = "signin"; }

There is a right way to do so or I’m doing right? (sorry for being that noob)

Regards,

Tiago Schmidt

Just got the real problem using the loggedInUser, when I try to retrieve the CurrentUser data, brings null and the loggedInUser gives me only the user’s object id, right?

How do I fix to use a real token in this case?

Regards,

Tiago Schmidt

Take a look at this:

https://jsfiddle.net/backendless/11316Lmt/

Open the browser (Chrome) console and run the program a couple of times. Let me know if you have any questions.

Regards,
Mark

Awesome! Works perfectly!

Thanks, Mark!

Best regards,

Tiago Schmidt

Hi,

I’m using the js sdk and my scenario is login page -> redirects to main page.
In the main page I’m trying to get the current user and get undefined or null depending on the method I use.
Backendless.UserService.getCurrentUser(); or
Backendless.LocalCache.get(“current-user”). I use persistent login. I currently work with localhost and bower when developing.
Can you please advice?

Hi Sharon,

how long ago have you updated the JS-SDK? This bug was fixed a few weeks ago, so maybe you just have an old version of SDK. Also use Backendless.LocalCache.get(“user-token”) to get user’s token instead of using Backendless.LocalCache.get(“current-user”). And for getting an object of current user use Backendless.UserService.getCurrentUser() (it should work fine).
Try to update a library and let me know if everything ok.

Regards,
Stanislav