Identify is a module for Stellar that allows developers to add authentication feature to their projects. This is a DRY approach on features like account creation, login, logout, confirmation by e-mail, password reset, and so on.
In order to maintain a secure connection between the server and the client, Identify uses the JSON Web Token (JWT) standard. This document is a specification for this module, describing in detail all the provided features and their implementation.
Represents a user on the database, it contains the following fields:
- name: User name (
string,default=''); - email: User email, will be used on the login process (
string,required); - password:User acess password, used in the login process (
string,required); - resetToken: Used to store the reset token when the user reset her password (
string,defaultsTo=null); - resetTokenExpire: Used to store the expire time of the reset token (
datetime,defaultsTo=null); - active: Indicates if the account is active or not (
boolean,defaultsTo=false); - shortName:Automatic field that is generated using the first name and the last name from the
namefield (string,computed); - metadata:Used to store additional user information like personal configurations, using a hash (
object,defaultsTo={}).
The developer can extend the model by using the system events. In this case using the core.models.add.user.
This configuration allows the developer to set if the user account is active or not by default. By default the value is set to false.
This configuration contains how long the token is valid. Accepts values in milliseconds. By default is set to 1440000 (1 day).
This configuration is mandatory in order to send reset and activation links to the user by email. By default, this is set to null.
This configuration allows set the time passed until the token expires. This can be expressed in seconds or a string describing a time span matching the format specified by the node package ms.
In this section are defined all the error messages used in the module. This messages must be specified using the configuration system, in order to allow to be customized by the developer. The only thing that should be constant is the error ID. The ID correspond to the key on the following list.
UserAlreadyExistsError: The user email is already in user.InvalidCredentialsError: Invalid credentials.InactiveAccountError: The account is inactive. Please, check your email for the activation link or request a new activation link.TokenExpiredError: The token expired.MalformedTokenError: The token is invalid, and isn't complaint with the JSON Web Token standard.
This action is responsible to create a new user.
name (string): user real name.email (string, required): must contain the user login email.password (string, required, min:6):must contain a clear text version of the user login password.`
This action must run these steps:
- If there is already an user with the same
email, on the database, theUserAlreadyExistsErrormust be thrown. - The
passwordinput is hashed. - The event
identify.beforeRegisteris executed and the input parameters is passed as parameter. - The
activefield is set with theidentify.activeByDefaultconfig. - If the
activeflag isfalse- Generate an activation token with expire time based on the
identify.activationTokenDurationconfig. - Set the
resetTokenand theresetTokenExpireon user data. - Send an email with the action link to the given
email. The link must be the URL defined on theidentify.activationLinkwith the query strings token set with theactivationToken, and one step set with the valueactivation.
- Generate an activation token with expire time based on the
- Create the user on the database.
- Execute the
identify.afterRegisterand pass the created model as parameter. - Return a success response with a
userfield that contains the created user model.
Get's a valid token that will be used by the user to identify themselves.
email (required, string): user email.password (required, string, min:6): clear text password for the given email.
This action must run these steps:
- If there is no registered user with the given
email, theInvalidCredentialsErrormust be thrown. - If the
activefield is set tofalse, thrown theInactiveAccountError. - If the hashed password (input field) doesn't match with the
passwordfield of the found user, throw theInvalidCredentialsError. - Generate a new valid JSON Web Token.
- That expires passed the time set on the
identify.expiresInconfiguration. - That contains the user model.
- That expires passed the time set on the
- Remove the
passwordfield from the output data. - Execute the event
identify.afterLoginwith the authenticated user and the created token. - Return a success message with the logged user and the valid token.
Disable an user account.
id (required, string): user identifier.
This action must run these steps:
- If there is no registered user with the given
id, theInvalidCredentialsErrormust be thrown. - Set the
activefield tofalseand save the change on the database. - Return a success message with the user as a response field.
Validate the token and return the user model if this one is valid.
token (required, string): token got using theidentify.loginaction.
This action must run these steps:
- Decode the given
token. - If the token has expired throw the
TokenExpiredError. - If is an invalid token throw the
MalformedTokenError. - If the user doesn't exists throw the
InvalidCredentialsError. - Append the expire timestamp on the response message (
expiresAt). - Fire the
identify.afterCheckSessionevent in order to other modules extend the response message. This must contain the response object as parameter.