You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 18, 2020. It is now read-only.
-[How do you log out a user?](https://github.com/strongloop/loopback-example-user-management#how-do-you-log-out-a-user)
14
15
-[How do you perform a password reset for a registered user](https://github.com/strongloop/loopback-example-user-management#how-do-you-perform-a-password-reset-for-a-registered-user)
15
16
16
-
######Notes
17
+
######Notes
17
18
- You will need to [configure LoopBack to send email](https://docs.strongloop.com/display/public/LB/Email+connector) for email related features
18
19
- If you're using GMail, you can simply [replace the user and pass](https://github.com/strongloop/loopback-example-user-management/blob/master/server/datasources.json#L19-L20) with your own credentials.
19
20
- 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](https://support.google.com/accounts/answer/6010255) for more information.
20
21
21
-
#Project Layout
22
+
## Project Layout
22
23
-`common/models` contains the extended user files. `user.js` contains user the logic for sending emails and password reset, while `user.json` contains the model definition.
23
24
-`server/boot/authentication.js` enables 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.js` file, see directly below).
24
25
-`server/boot/routes.js` contains all the routing logic. In this example, we have used [ExpressJS](http://expressjs.com/) to configure the routing since each LoopBack app is an extended version of an Express app.
25
26
-`server/views`contains all the views (or pages) rendered by Express using the [EJS templating framework](http://www.embeddedjs.com/)
26
27
-`server/datasources.json` contains the datasource configurations. Here is where we add an email datasource.
27
28
-`server/model-config.json` contains 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.
28
29
29
-
######Note
30
+
######Note
30
31
All other files have not been modified from their defaults.
31
32
32
-
#How do you register a new user?
33
+
## How do you register a new user?
33
34
1. Create a [form](https://github.com/strongloop/loopback-example-user-management/blob/master/server/views/login.ejs#L21-L36) to gather sign up information
34
35
2. Create a [remote hook](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L5-L35) to [send a verification email](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L9-L34)
35
36
36
-
######Notes
37
+
######Notes
37
38
- Upon execution, [`user.verify`](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L19) sends an email using the provided [options](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L9-L17)
38
39
- The verification email is configured to [redirect the user to the `/verified` route](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L15) in our example. For your app, you should configure the redirect to match your use case
39
40
- The [options](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L9-L17) are self-explanitory except `type`, `template` and `user`
40
41
-`type` - value must be `email`
41
42
-`template` - the path to the template to use for the verification email
42
43
-`user` - when provided, the information in the object will be used in the verification link email
43
44
44
-
#How do you send an email verification for a new user registration?
45
+
## How do you send an email verification for a new user registration?
45
46
See [step 2](https://github.com/strongloop/loopback-example-user-management#how-do-you-register-a-new-user) in the previous question
46
47
47
-
#How do you log in a user?
48
+
## How do you log in a user?
48
49
1. Create a [form to accept login credentials](https://github.com/strongloop/loopback-example-user-management/blob/master/server/views/login.ejs#L2-L17)
49
50
2. Create an [route to handle the login request](https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L20-L41)
50
51
51
-
#How do you log out a user?
52
+
## How do you log out a user?
52
53
1. Create a [logout link with the access token embedded into the URL](https://github.com/strongloop/loopback-example-user-management/blob/master/server/views/home.ejs#L4)
53
54
2. Call [`User.logout`](https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L45) with the access token
54
55
55
-
######Notes
56
+
######Notes
56
57
- We use the LoopBack token middleware to process access tokens. As long as you provide `access_token` in the query string of URL, the access token object will be provided in `req.accessToken` property in your route handler
57
58
58
-
#How do you perform a password reset for a registered user?
59
+
## How do you perform a password reset for a registered user?
59
60
1. Create a [form to gather password reset info](https://github.com/strongloop/loopback-example-user-management/blob/master/server/views/login.ejs#L40-L51)
60
61
2. Create an [endpoint to handle the password reset request](https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L52-L66). Calling `User.resetPassword` ultimately emits a `resetPasswordRequest` event and creates a temporary access token
61
62
3. Register an event handler for the `resetPasswordRequest` that sends an email to the registered user. In our example, we provide a [URL](https://github.com/strongloop/loopback-example-user-management/blob/master/common/models/user.js#L40-L41) that redirects the user to a [password reset page authenticated with a temporary access token](https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L68-L74)
0 commit comments