This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
routes.js
93 lines (84 loc) · 2.66 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright IBM Corp. 2014,2017. All Rights Reserved.
// Node module: loopback-example-user-management
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var dsConfig = require('../datasources.json');
var path = require('path');
module.exports = function(app) {
var User = app.models.user;
//login page
app.get('/', function(req, res) {
var credentials = dsConfig.emailDs.transports[0].auth;
res.render('login', {
email: credentials.user,
password: credentials.pass
});
});
//verified
app.get('/verified', function(req, res) {
res.render('verified');
});
//log a user in
app.post('/login', function(req, res) {
User.login({
email: req.body.email,
password: req.body.password
}, 'user', function(err, token) {
if (err) {
if(err.details && err.code === 'LOGIN_FAILED_EMAIL_NOT_VERIFIED'){
res.render('reponseToTriggerEmail', {
title: 'Login failed',
content: err,
redirectToEmail: '/api/users/'+ err.details.userId + '/verify',
redirectTo: '/',
redirectToLinkText: 'Click here',
userId: err.details.userId
});
} else {
res.render('response', {
title: 'Login failed. Wrong username or password',
content: err,
redirectTo: '/',
redirectToLinkText: 'Please login again',
});
}
return;
}
res.render('home', {
email: req.body.email,
accessToken: token.id,
redirectUrl: '/api/users/change-password?access_token=' + token.id
});
});
});
//log a user out
app.get('/logout', function(req, res, next) {
if (!req.accessToken) return res.sendStatus(401);
User.logout(req.accessToken.id, function(err) {
if (err) return next(err);
res.redirect('/');
});
});
//send an email with instructions to reset an existing user's password
app.post('/request-password-reset', function(req, res, next) {
User.resetPassword({
email: req.body.email
}, function(err) {
if (err) return res.status(401).send(err);
res.render('response', {
title: 'Password reset requested',
content: 'Check your email for further instructions',
redirectTo: '/',
redirectToLinkText: 'Log in'
});
});
});
//show password reset form
app.get('/reset-password', function(req, res, next) {
if (!req.accessToken) return res.sendStatus(401);
res.render('password-reset', {
redirectUrl: '/api/users/reset-password?access_token='+
req.accessToken.id
});
});
};