Retrieving user object from user-token | Backend as a Service Platform

Message:

Subscribe rss
Blog categories
All | Backendless features
Retrieving user object from user-token
June 5, 2018 by Sergey Kukurudzyak

Suppose your app logs in a user. As a result, the app gets 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 user-token. This could happen if you used persistent login in the application, which stores 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, I will show you how to do this.

The technique for retrieving the user object is creating an API service which accepts a  user-token in the header and retrieves the current user. I will use Codeless to create the API service because it has an intuitive interface and allows you to solve these tasks very quickly, just by building the algorithms instead of writing code:

  1. Go to the Backendless console and choose the Business Logic section from the left menu, then go to the Codeless tab and create a service.
  2. Create а method with the name retreiveUser:
    Create Service 1024x728 - Retrieving user object from user-token
  3. Drag and drop the getCurrentUser block from the Backendless -> Users API section as shown below:
    add get current user - Retrieving user object from user-token
  4. Press the DEPLOY MODEL button.
  5. Once it is done, go to the Data section and add a user (you would also need to set up the password for that user):save user 1024x721 - Retrieving user object from user-token
  6. Once the user is created, go back to the API SERVICES tab in Business Logic, select the UserHelper service and choose the  retrieveUser method:login user 1024x638 - Retrieving user object from user-token
  7.  Fill in login and password for the user which was created in step 5 and click the Login button.
  8. Click the INVOKE button:invoke 1024x456 - Retrieving user object from user-tokenAs a result, we get a response, which is a complete user object. So, by having a user token, you are able to do pretty much anything with that user object.
Share this post
Tweet about this on TwitterShare on FacebookGoogle+