Suppose your app logs in a user. As a result, the app gets a user-token which uniquely identifies the user’s session with Backendless. If your app uses our SDK for Android, iOS, JS or .NET, the user-token value is managed directly by our libraries. Specifically, it is added to every API call to maintain the session and tell the server about the user’s identity.
There are situations when you need to get the user object when your app has only the user-token. This could happen if you used persistent login in the application, which stores the user-token on the device. The implementation does not save the user object; however, there is a way to retrieve the user based on the user-token value (assuming the token is still valid). In this article, we will show you how to do this.
The technique for retrieving the user object is creating an API service that accepts a user-token in the header and retrieves the current user. We will use Codeless to create the API service because it has an intuitive interface and allows you to solve these tasks very quickly simply by building the algorithms instead of writing code.
This is exactly what I’m trying to do in cloud javascript in a javascript based service. What would be the way to accomplish this in javascript and not code-less ?
Hi John,
To do it with JS, you’d need to create an API service. In your service implementation, you’d need to use “this.request.context.userId” from the invocation context object (https://backendless.com/docs/bl-js/bl_invocation_context.html). Once you have user’s id (which is the objectId value assigned to the user), you can retrieve the user fro the Users table using the “findById” API call (https://backendless.com/docs/js/data_basic_search.html)
Hope this helps.
Regards,
Mark