Hello,
I’m developing an app using Backendless JS SDK.
I’m trying to implement user persistence among app restarts but I’m being unable of. I’ve been reading other topics about the subject, and have downloaded the latest SDK. The code about user persistence is as follows:
if (Backendless.LocalCache.get(“user-token”)) {
Backendless.UserService.isValidLogin(new Backendless.Async(function(data) {
var currentUserObjectId = Backendless.UserService.loggedInUser();
console.log("currentUserObjectId="+currentUserObjectId);
_user = Backendless.Data.of(Backendless.User).findById(currentUserObjectId);
...
}
}, function(error) {
...
}));
}
Backendless.UserService.loggedInUser() always returns null or undefined, and I get the following error: “Uncaught Error: Invalid value for the “value” argument. The argument must contain only string or object values, http://localhost:8100/lib/backendless.js, Line: 1025”
This is a showstopper for me right now. Can you please provide a solution or workaround?
Thanks in advance,
Luís
Hi Guys,
Can you please help me here? I’m stuck on this issue, and can’t move forward.
I’ve tried a lot of alternatives like Backendless.LocalCache.get(“user-id”) but nothing seems to work.
I’m using the latest SDK version.
Thanks,
Luís
Sorry, I removed my post as I saw later that you wrote you’re using the latest.
Maybe you could prepare some self-contained sample, which reproduces the problem? So that I could just copy it and run in the browser.
I can’t do that Sergey, since I’m building an app using the Ionic framework and it’s huge by now, with a lot of integrations and plugins.
But testing it on Chrome, I can see the user-token and stayLoggedIn keys in the Local Cache.
But I can send you portions of the code.
We currently do not support cache in Ionic Framework. But this feature is already in progress and we shall notify you here as soon as it is available.
Ok, thanks Sergey.
Please do let me know when the feature is available.
Hi Luís,
Try using
Backendless.UserService.getCurrentUser()
instead of
Backendless.UserService.loggedInUser()
Mark
Hi Mark,
It returns an object with getCurrentUser, but it’s empty, I can’t retrieve any property from it.
Luís
Hi Luis!
Already answered here http://support.backendless.com/t/user-persistence-when-app-restarts
for your question. We need some time to add compatibility with ionic framework in our sdk.
Regards,
Kate.
Hi Kate,
I have a very similar issue. I have a function that is executed every time a page loads to obtain the current user (by the way, I did it based on a previous question answered by you in this forum):
function getUserSession() {
var cache = Backendless.LocalCache.getAll();
if (cache["stayLoggedIn"]) {
var tokenExist = Backendless.UserService.isValidLogin();
if (!tokenExist) {
Backendless.LocalCache.clear();
}
var usr = Backendless.UserService.getCurrentUser();
return usr;
}
return null;
}
The wierd thing about it, is that somtiemes work and other dont. When it does not work gives me this error: “Uncaught TypeError: Cannot read property ‘isValidLogin’ of undefined”
But if I reload the page, or if I go to the javascript console in the browser and execute this function the same line works! It`s a random error. Sometimes works perfect sometimes no. Please help me!
I decide to use the Backendless javascript API a lot in my project and this is an stoper.
Thanks in advance very very much for your help
Hi Alejandro,
the error you encounter means that Backendless object was not initialized before calling isValidLogin
. Make sure that you always call Backendless.init(...)
function before any other requests to Backendless.
Regards,
Stanislav