Hi, I am trying to login my user and keep it loggedin using this code:
Backendless.UserService.login(req.body.email, req.body.password, true).then(function(userLogged){
}).catch(function(error){
});
The call never returns. How can I fix this to make it work and obtain a valid user-token?
Hi Mark
Thank you for you quick answer. Yes I did. I am using promises in other parts of my server and they are working fine but here
Hi George,
Here’s an example and it is working just fine. You can run it an experience yourself:
https://jsfiddle.net/19z26967/
Mark
I need to keep the user logged in, I don’t see there in the code where are you passing true to enable the stayLoggedin functionality.
Well, now it does. Same result - works as just fine:
https://jsfiddle.net/19z26967/1/
Ok perfect, Thanks
Another question, I added this to the success call in your example:
var userToken = Backendless.LocalCache.get(“current-user-token”);
alert(userToken);
and it’s showing “undefined”. Why? How can I get the user token with this method in javascript from nodejs?
I do not think the local storage option with nodejs would work. Are you going to use as custom business logic in Backendless? In that case, the persistent login would not be applicable as there are no guarantees that subsequent invocation will be handled by the same instance of the node process.
no business logic for now. I need to get this token to upload the user picture. Since the sdk does not support yet this functionality from node js I will do it using the REST API
You do not need persistent login then. Just do this after the login is done to get the user token:
Backendless.UserService.getCurrentUser()[“user-token”]
I’ve done that in the past and it’s not working. I get “undefined”. I also tried in your example and same result: “undefined”
hi George,
As I understand you want to get a user-token on the client, not on the server (via nodeJS) right?
if true you need to use key “user-token” instead “current-user-token” for getting token from localStorage
var userToken = Backendless.LocalCache.get("user-token");
alert(userToken);
Hope this will help you.
Vladimir,
Hi Vladimir
I want to get the user token from the “nodejs” side. not from the client. I’ve tried “user-token” and “current-user-token” and all of them return “undefined”. I also tried the REST API and after the login is done a valid token is sent, but I would like to keep javascript code as much as i can. I f theres no way to get the user token from the nodejs side after a login is done, then I will keep the REST API call.
Hi George,
Please see the changes here:
https://jsfiddle.net/19z26967/4/
With the overrides for Backendless.LocalCache, it now works and you can now get a hold of user-token.
Mark
Thank you so much Mark, it worked perfectly. I would never figure out this logic.