-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathloginController.js
More file actions
171 lines (144 loc) · 5.63 KB
/
loginController.js
File metadata and controls
171 lines (144 loc) · 5.63 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// import module `bcrypt`
const bcrypt = require('bcrypt');
// import module `database` from `../models/db.js`
const db = require('../models/db.js');
// import module `User` from `../models/UserModel.js`
const User = require('../models/UserModel.js');
/*
defines an object which contains functions executed as callback
when a client requests for `login` paths in the server
*/
const loginController = {
/*
executed when the client sends an HTTP GET request `/login`
as defined in `../routes/routes.js`
*/
getLogIn: function (req, res) {
// checks if a user is logged-in by checking the session data
if(req.session.idNum) {
/*
redirects the client to `/profile` using HTTP GET,
defined in `../routes/routes.js`
passing values using URL
which calls getProfile() method
defined in `./profileController.js`
*/
res.redirect('/profile/' + req.session.idNum);
}
// else if a user is not yet logged-in
else {
/*
sets `details.flag` to false
to hide the profile and logout tabs in the nav bar
*/
var details = {
flag: false
};
// render `../views/login.hbs`
res.render('login', details);
}
},
/*
executed when the client sends an HTTP POST request `/login`
as defined in `../routes/routes.js`
*/
postLogIn: function (req, res) {
/*
when submitting forms using HTTP POST method
the values in the input fields are stored in `req.body` object
each <input> element is identified using its `name` attribute
Example: the value entered in <input type="text" name="idNum">
can be retrieved using `req.body.idNum`
*/
var idNum = req.body.idNum;
var pw = req.body.pw;
/*
calls the function findOne()
defined in the `database` object in `../models/db.js`
this function finds a document from collection `users`
where `idNum` is equal to `idNum`
*/
db.findOne(User, {idNum: idNum}, '', function (result) {
// if a user with `idNum` equal to `idNum` exists
if(result) {
var user = {
fName: result.fName,
lName: result.lName,
idNum: result.idNum
};
/*
use compare() method of module `bcrypt`
to check if the password entered by the user
is equal to the hashed password in the database
*/
bcrypt.compare(pw, result.pw, function(err, equal) {
/*
if the entered password
match the hashed password from the database
*/
if(equal) {
/*
stores `user.idNum` to `req.session.idNum`
stores `user.fName` to `req.session.name`
these values are stored to the `req.session` object
to indicate that a user is logged-in
these values will be removed
if the user logs-out from the web application
*/
req.session.idNum = user.idNum;
req.session.name = user.fName;
/*
redirects the client to `/profile/idNum`
where `idNum` is equal
to the `idNum` entered by the user
defined in `../routes/routes.js`
which calls getProfile() method
defined in `./profileController.js`
*/
res.redirect('/profile/' + user.idNum);
}
/*
else if the entered password
does not match the hashed password from the database
*/
else {
/*
sets `details.flag` to false
to hide the profile and logout tabs in the nav bar
*/
var details = {
flag: false,
error: `ID Number and/or Password is incorrect.`
};
/*
render `../views/login.hbs`
display the errors
*/
res.render('login', details);
}
});
}
// else if a user with `idNum` equal to `idNum` does not exist
else {
/*
sets `details.flag` to false
to hide the profile and logout tabs in the nav bar
*/
var details = {
flag: false,
error: `ID Number and/or Password is incorrect.`
};
/*
render `../views/login.hbs`
display the errors
*/
res.render('login', details);
}
});
}
}
/*
exports the object `loginController` (defined above)
when another script exports from this file
*/
module.exports = loginController;