-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsignupController.js
More file actions
140 lines (117 loc) · 4.55 KB
/
signupController.js
File metadata and controls
140 lines (117 loc) · 4.55 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
// import module `validationResult` from `express-validator`
const { validationResult } = require('express-validator');
// 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 `signup` paths in the server
*/
const signupController = {
/*
executed when the client sends an HTTP GET request `/signup`
as defined in `../routes/routes.js`
*/
getSignUp: function (req, res) {
res.render('signup');
},
/*
executed when the client sends an HTTP POST request `/signup`
as defined in `../routes/routes.js`
prior to the execution of this function, values are validated
with an array of validation middlewares
defined in the object `validation` in `../helpers/validation.js`
*/
postSignUp: async function (req, res) {
// checks if there are validation errors
var errors = validationResult(req);
// if there are validation errors
if (!errors.isEmpty()) {
// get the array of errors
errors = errors.errors;
/*
for each error, store the error inside the object `details`
the field is equal to the parameter + `Error`
the value is equal to `msg`
as defined in the validation middlewares
for example, if there is an error for parameter `fName`:
store the value to the field `fNameError`
*/
var details = {};
for(i = 0; i < errors.length; i++)
details[errors[i].path + 'Error'] = errors[i].msg;
/*
render `../views/signup.hbs`
display the errors defined in the object `details`
*/
res.render('signup', details);
}
else {
/*
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="fName">
can be retrieved using `req.body.fName`
*/
var fName = req.body.fName;
var lName = req.body.lName;
var idNum = req.body.idNum;
var pw = req.body.pw;
var user = {
fName: fName,
lName: lName,
idNum: idNum,
pw: pw
}
/*
calls the function insertOne()
defined in the `database` object in `../models/db.js`
this function adds a document to collection `users`
*/
var response = await db.insertOne(User, user);
/*
upon adding a user to the database,
redirects the client to `/success` using HTTP GET,
defined in `../routes/routes.js`
passing values using URL
which calls getSuccess() method
defined in `./successController.js`
*/
if(response != null){
res.redirect('/success?fName=' + fName +'&lName=' + lName + '&idNum=' + idNum);
}
else {
res.render('error');
}
}
},
/*
executed when the client sends an HTTP GET request `/getCheckID`
as defined in `../routes/routes.js`
*/
getCheckID: async function (req, res) {
/*
when passing values using HTTP GET method
the values are stored in `req.query` object
Example url: `http://localhost/getCheckID?idNum=11312345`
To retrieve the value of parameter `idNum`: `req.query.idNum`
*/
var idNum = req.query.idNum;
/*
calls the function findOne()
defined in the `database` object in `../models/db.js`
searches for a single document based on the model `User`
sends an empty string to the user if there are no match
otherwise, sends an object containing the `idNum`
*/
var result = await db.findOne(User, {idNum: idNum}, 'idNum');
res.send(result);
}
}
/*
exports the object `signupController` (defined above)
when another script exports from this file
*/
module.exports = signupController;