Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit fbead1d

Browse files
authored
Merge pull request #1 from rohan-ramakrishnan/master
Email verification + password reset functionality
2 parents 2efc250 + fe9ad1d commit fbead1d

File tree

4 files changed

+228
-23
lines changed

4 files changed

+228
-23
lines changed

controllers/auth.controllers.js

Lines changed: 107 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
const nodeMailer = require("nodemailer");
1+
const sgMail = require("@sendgrid/mail");
22

33
const User = require("../models/user.model");
44

55
const generateToken = require("../utils/authMethods.utils").generateAccessToken;
66
const hashPassword = require("../utils/authMethods.utils").hashPassword;
77
const checkPassword = require("../utils/authMethods.utils").checkPassword;
88

9-
const transporter = nodeMailer.createTransport({
10-
service: "gmail",
11-
auth: {
12-
user: process.env.GMAIL_USER,
13-
pass: process.env.GMAIL_PASS,
14-
},
15-
});
16-
9+
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
1710
exports.postLoginController = (req, res) => {
1811
const email = req.body.email;
1912
const password = req.body.password;
@@ -34,7 +27,11 @@ exports.postLoginController = (req, res) => {
3427
signed: true,
3528
})
3629
.status(200)
37-
.json({ success: true, username: users.username });
30+
.json({
31+
success: true,
32+
username: users.username,
33+
active: users.active,
34+
});
3835
})
3936
.catch((err) => {
4037
console.log(err);
@@ -96,21 +93,21 @@ exports.postRegisterController = (req, res) => {
9693
user
9794
.save()
9895
.then((result) => {
99-
const host = req.get("host");
100-
const link = `http://${host}/verify?token=${verifyToken}`;
101-
const mailOptions = {
102-
from: "noreply@techoptimum.org",
96+
const link = `http://localhost:3001/verify/${verifyToken}`;
97+
const msg = {
10398
to: email,
104-
subject: "Verify your email",
105-
html: `<h1>Verify your email</h1><br><a href="${link}">Click here to verify your email</a>`,
99+
from: process.env.FROM_EMAIL,
100+
subject: "Verify your Tech Optimum Account",
101+
html: `<h1>Verify your Tech Optimum Account.</h1><br><a href="${link}">Click here to verify your email.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
106102
};
107-
transporter.sendMail(mailOptions, (err, info) => {
108-
if (err) {
103+
sgMail
104+
.send(msg)
105+
.then((result) => {
106+
console.log("Email sent.");
107+
})
108+
.catch((err) => {
109109
console.log(err);
110-
} else {
111-
console.log(info);
112-
}
113-
});
110+
});
114111
return res
115112
.cookie("token", token, {
116113
maxAge: 1000 * 60 * 60,
@@ -121,6 +118,7 @@ exports.postRegisterController = (req, res) => {
121118
.json({
122119
success: true,
123120
username,
121+
active: false,
124122
});
125123
})
126124
.catch((err) => {
@@ -169,8 +167,16 @@ exports.getVerifyController = (req, res) => {
169167
token: token,
170168
})
171169
.then((users) => {
170+
if (users.length < 1) {
171+
return res.json({
172+
success: false,
173+
msg: "Token not recognized.",
174+
errType: "tknnr",
175+
});
176+
}
172177
if (users.active === false) {
173178
users.active = true;
179+
users.token = null;
174180
users
175181
.save()
176182
.then((result) => {
@@ -191,6 +197,7 @@ exports.getVerifyController = (req, res) => {
191197
return res.status(200).json({
192198
success: true,
193199
msg: "Email already verified.",
200+
code: "emav",
194201
});
195202
}
196203
})
@@ -203,3 +210,81 @@ exports.getVerifyController = (req, res) => {
203210
});
204211
});
205212
};
213+
214+
exports.postFPassReq = (req, res) => {
215+
const email = req.body.email;
216+
const verifyToken = require("crypto").randomBytes(64).toString("hex");
217+
User.findOne({ email }).then((users) => {
218+
if (!users) {
219+
return res.json({
220+
success: false,
221+
code: "emalnex",
222+
});
223+
} else {
224+
users.token = verifyToken;
225+
users.save().then((result) => {
226+
const link = `http://localhost:3001/verify/reset-password/${verifyToken}`;
227+
const msg = {
228+
to: email,
229+
from: process.env.FROM_EMAIL,
230+
subject: "Password Reset Requested.",
231+
html: `<h1>You requested a password reset.</h1><br><a href="${link}">Click here to continue.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
232+
};
233+
sgMail
234+
.send(msg)
235+
.then((result) => {
236+
console.log("Email sent.");
237+
})
238+
.catch((err) => {
239+
console.log(err);
240+
});
241+
return;
242+
}).then(() => {
243+
res.json({
244+
success: true,
245+
});
246+
})
247+
}
248+
});
249+
};
250+
251+
exports.postResetPassword = (req, res) => {
252+
const token = req.query.token;
253+
const password = req.body.password;
254+
User.findOne({
255+
token,
256+
}).then((user) => {
257+
if (!user) {
258+
return res.json({
259+
success: false,
260+
code: "nuf",
261+
});
262+
} else {
263+
if (user.token.toString() === token.toString()) {
264+
hashPassword(password)
265+
.then((hashedPassword) => {
266+
user.password = hashedPassword;
267+
user.token = null;
268+
return user.save();
269+
})
270+
.then((result) => {
271+
const msg = {
272+
to: user.email,
273+
from: process.env.FROM_EMAIL,
274+
subject: "Password Reset Successful.",
275+
html: `<h1>Your password was reset successfully.</h1><br><br><h3>Sincerely, Tech Optimum</h3>`,
276+
};
277+
sgMail
278+
.send(msg)
279+
.then((result) => {
280+
console.log("Email sent.");
281+
})
282+
.catch((err) => {
283+
console.log(err);
284+
});
285+
return res.json({ success: true, msg: "Password changed." });
286+
});
287+
}
288+
}
289+
});
290+
};

package-lock.json

Lines changed: 116 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3+
"@sendgrid/mail": "^7.7.0",
34
"bcrypt": "^5.0.1",
45
"body-parser": "^1.20.0",
56
"cookie-parser": "^1.4.6",

routes/auth.routes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ router.post('/logout', authControllers.postLogoutController);
1212

1313
router.get('/verify', authControllers.getVerifyController);
1414

15+
router.post('/reset-password-request', authControllers.postFPassReq);
16+
17+
router.post('/reset-password', authControllers.postResetPassword);
18+
1519
module.exports = router;

0 commit comments

Comments
 (0)