Page Contents

The basic process for an application to create and authenticate users is:

  1. Register a new user with the User.create() method, inherited from the PersistedModel object. See Registering users for details.
  2. Call User.login() to request an access token from the client application on behalf of the user. See Logging in users for details.
  3. Invoke an API using the access token. Provide the access token in the HTTP header or as a query parameter to the REST API call, as illustrated below.

Making authenticated requests with access tokens

Once a user is logged in, LoopBack creates a new AccessToken referencing the user. This token is required when making subsequent REST requests for the access control system to validate that the user can invoke methods on a given Model.



# Authorization Header
curl -X GET -H "Authorization: $ACCESS_TOKEN" \

# Query Parameter
curl -X GET http://localhost:3000/api/widgets?access_token=$ACCESS_TOKEN

To use cookies for authentication, add the following to server.js (before boot):


    model: app.models.accessToken

Enable vanity user URLs

To display vanity user URLs, configure the token middleware with currentUserLiteral options. 


    model: app.models.accessToken,
    currentUserLiteral: 'me'

The currentUserLiteral defines a special token that can be used in the URL for REST APIs, for example:

curl -X GET http://localhost:3000/api/users/me/orders?access_token=$ACCESS_TOKEN

Please note the URL will be rewritten to http://localhost:3000/api/users/<currentLoggedInUserId>/orders?access_token=$ACCESS_TOKEN by LoopBack.

Deleting access tokens

A user will be effectively logged out by deleting the access token they were issued at login. This affects only the specified access token; other tokens attached to the user will still be valid.


var USER_ID = 1;
// remove just the token
var token = new AccessToken({
// remove all user tokens
  where: {
    userId: USER_ID