$ git clone email@example.com:strongloop/loopback-example-user-management.git $ cd loopback-example-user-management $ npm install $ node .
- Project Layout
- How do you register a new user?
- How do you send an email verification for a new user registration?
- How do you log in a user?
- How do you log out a user?
- How do you perform a password reset for a registered user
- You will need to configure LoopBack to send email for email related features
- If you’re using GMail, you can simply replace the user and pass with your own credentials.
- With GMail, you might need to temporarily allow “less secure” apps to access you email account. See Allowing less secure apps to access your account for more information.
common/modelscontains the extended user files.
user.jscontains the logic for sending emails and password reset, while
user.jsoncontains the model definition.
server/boot/authentication.jsenables authentication middleware with the
enableAuth()method. It’s this middleware that finds the access token id string (usually from the query string) and appends entire token instance onto the express request object as
req.accessToken. From there, you can find the user’s ID:
req.accessToken.userId(used in the
routes.jsfile, see directly below).
server/boot/routes.jscontains all the routing logic. In this example, we have used Express to configure the routing since each LoopBack app is an extended version of an Express app.
server/viewscontains all the views (or pages) rendered by Express using the EJS templating framework
server/datasources.jsoncontains the datasource configurations. Here is where we add an email datasource.
server/model-config.jsoncontains the all the model configurations. Here is where we configure the extended user model (lowercase ‘u’) and the email model. The rest of the models are all built-in LoopBack models.
All other files have not been modified from their defaults.
How do you register a new user?
- Upon execution,
user.verifysends an email using the provided options
- The verification email is configured to redirect the user to the
/verifiedroute in our example. For your app, you should configure the redirect to match your use case
- The options are self-explanatory except
type- value must be
template- the path to the template to use for the verification email
user- when provided, the information in the object will be used in the verification link email
How do you send an email verification for a new user registration?
See step 2 in the previous question
How do you log in a user?
How do you log out a user?
- Create a logout link with the access token embedded into the URL
User.logoutwith the access token
- We use the LoopBack token middleware to process access tokens. As long as you provide
access_tokenin the query string of URL, the access token object will be provided in
req.accessTokenproperty in your route handler
How do you perform a password reset for a registered user?
- Create a form to gather password reset info
- Create an endpoint to handle the password reset request. Calling
User.resetPasswordultimately emits a
resetPasswordRequestevent and creates a temporary access token
- Register an event handler for the
resetPasswordRequestthat sends an email to the registered user. In our example, we provide a URL that redirects the user to a password reset page authenticated with a temporary access token
- Create a password reset form for the user to enter and confirm their new password
- Create an endpoint to process the password reset
- For the
resetPasswordRequesthandler callback, you are provided with an
infoobject which contains information related to the user that is requesting the password reset. Note that this example is set up to send an initial email to yourself (the FROM and TO fields are the same). You will eventually want to change the address in the FROM field.