-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.mjs
166 lines (155 loc) · 4.57 KB
/
auth.mjs
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
import bcrypt from 'bcryptjs';
import mongoose from 'mongoose';
// assumes that User was registered in `./db.mjs`
const User = mongoose.model('User');
// unit test
// const changepassword=(newpassword, error)=>{
// if (newpassword.length < 8){
// error("USERNAME PASSWORD TOO SHORT");
// }
// else{
// User.findOne({username: req.body.username}).exec((err, user) => {
// if (err) {
// error("FINDONE ERROR");
// }
// if(user === null){
// error("CANNOT FIND USER");
// }
// else {
// bcrypt.compare(req.body.oldpassword, user.password, (err, passwordMatch) => {
// // regenerate session if passwordMatch is true
// if(err){
// console.log("PASSWORD FIND ERROR");
// }
// else if(!passwordMatch){
// console.log("PASSWORDS DO NOT MATCH");
// }
// else{
// bcrypt.hash(req.body.newpassword, 10, function(err, hashedNew) {
// if (err){
// console.log(err);
// }else{
// user.password = hashedNew;
// user.save(function(err2){
// if(err2){
// console.log("err2");
// res.send(err2);
// }
// });
// res.redirect("/");
// }
// });
// }
// });
// }
// });
// }
// };
const startAuthenticatedSession = (req, user, cb) => {
// TODO: implement startAuthenticatedSession
// assuming that user is the user retrieved from the database
req.session.regenerate((err) => {
if (!err) {
// set a property on req.session that represents the user
req.session.user = user;
} else {
// log out error
console.log(err);
}
// call callback with error
cb(err);
});
};
function endAuthenticatedSession(req, cb) {
req.session.destroy((err) => { cb(err); });
}
const register = (username, email, password, errorCallback, successCallback) => {
if (username.length < 8 || password.length < 8){
console.log("USERNAME PASSWORD TOO SHORT");
errorCallback({message: "USERNAME PASSWORD TOO SHORT"});
}else{
User.findOne({username: username}, (err, result) =>{
if (err){
console.log(err);
errorCallback({message: err.toString()});
}else if (result){
console.log("USERNAME ALREADY EXISTS");
errorCallback({message: "USERNAME ALREADY EXISTS"});
}else{
// you can use a default value of 10 for salt rounds
bcrypt.hash(password, 10, function(err, hash) {
if (err){
console.log(err);
errorCallback({message: err.toString()});
}else{
const newUser = new User({
username: username,
password: hash,
email: email
});
newUser.save(err => {
if (err){
console.log(err);
errorCallback({message: "DOCUMENT SAVE ERROR"});
}else{
successCallback(newUser);
}
});
}
});
}
});
}
};
const login = (username, password, errorCallback, successCallback) => {
// TODO: implement login
User.findOne({username: username}, (err, user) => {
if(err){
console.log("USER FIND ERROR");
errorCallback({message: "USER FIND ERROR"});
}
else if(!user) {
// compare with form password!
console.log("USER NOT FOUND");
errorCallback({message: "USER NOT FOUND"});
}
else{
bcrypt.compare(password, user.password, (err, passwordMatch) => {
// regenerate session if passwordMatch is true
if(err){
console.log("PASSWORD FIND ERROR");
errorCallback({message: "PASSWORD FIND ERROR"});
}
else if(!passwordMatch){
console.log("PASSWORDS DO NOT MATCH");
errorCallback({message: "PASSWORDS DO NOT MATCH"});
}
else{
successCallback(user);
}
});
}
});
};
// creates middleware that redirects to login if path is included in authRequiredPaths
const authRequired = authRequiredPaths => {
return (req, res, next) => {
if(authRequiredPaths.includes(req.path)) {
if(!req.session.user) {
res.redirect('/login');
} else {
next();
}
} else {
next();
}
};
};
export {
startAuthenticatedSession,
endAuthenticatedSession,
register,
login,
authRequired,
// changepassword
};