The basic process for an application to create and authenticate users is:
- Register a new user with the
User.create()method, inherited from thePersistedModelobject. See Registering users for details. - Call
User.login()to request an access token from the client application on behalf of the user. See Logging in users for details. - 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.
shell
ACCESS_TOKEN=6Nb2ti5QEXIoDBS5FQGWIz4poRFiBCMMYJbYXSGHWuulOuy0GTEuGx2VCEVvbpBK
# Authorization Header
curl -X GET -H "Authorization: $ACCESS_TOKEN" \
http://localhost:3000/api/widgets
# Query Parameter
curl -X GET http://localhost:3000/api/widgets?access_token=$ACCESS_TOKEN
To use cookies for authentication, add the following to middleware.json :
/server/middleware.json
{
"auth": {
"loopback#token": {}
}
}
or, the following to server.js (before boot):
/server/server.js
app.middleware('auth', loopback.token());
Note:
The Loopback Angular SDK doesn’t support using cookies, and expects you to be using an access token returned from User.login().
Enable vanity user URLs
To display vanity user URLs, configure the token middleware with currentUserLiteral options.
/server/middleware.json
{
"auth": {
"loopback#token": {
"params": {
"currentUserLiteral": "me"
}
}
}
}
or
/server/server.js
app.middleware('auth', loopback.token({
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.
/server/boot/script.js
var USER_ID = 1;
var ACCESS_TOKEN = '6Nb2ti5QEXIoDBS5FQGWIz4poRFiBCMMYJbYXSGHWuulOuy0GTEuGx2VCEVvbpBK';
// remove just the token
var token = new AccessToken({
id: ACCESS_TOKEN
});
token.destroy();
// remove all user tokens
AccessToken.destroyAll({
userId: USER_ID
});