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

Commit 9429434

Browse files
committed
sync
1 parent f32bb42 commit 9429434

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

controllers/auth.controllers.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
const nodeMailer = require("nodemailer");
2+
13
const User = require("../models/user.model");
24

35
const generateToken = require("../utils/authMethods.utils").generateAccessToken;
46
const hashPassword = require("../utils/authMethods.utils").hashPassword;
57
const checkPassword = require("../utils/authMethods.utils").checkPassword;
68

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+
717
exports.postLoginController = (req, res) => {
818
const email = req.body.email;
919
const password = req.body.password;
@@ -59,6 +69,7 @@ exports.postRegisterController = (req, res) => {
5969
const email = req.body.email;
6070
const password = req.body.password;
6171
const confirmPassword = req.body.confirmPassword;
72+
const verifyToken = require("crypto").randomBytes(64).toString("hex");
6273

6374
if (password === confirmPassword) {
6475
hashPassword(password).then((hashedPass) => {
@@ -79,10 +90,27 @@ exports.postRegisterController = (req, res) => {
7990
username,
8091
email,
8192
password: hashedPass,
93+
active: false,
94+
token: verifyToken,
8295
});
8396
user
8497
.save()
8598
.then((result) => {
99+
const host = req.get("host");
100+
const link = `http://${host}/verify?token=${verifyToken}`;
101+
const mailOptions = {
102+
from: "noreply@techoptimum.org",
103+
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>`,
106+
};
107+
transporter.sendMail(mailOptions, (err, info) => {
108+
if (err) {
109+
console.log(err);
110+
} else {
111+
console.log(info);
112+
}
113+
});
86114
return res
87115
.cookie("token", token, {
88116
maxAge: 1000 * 60 * 60,
@@ -134,3 +162,44 @@ exports.postRegisterController = (req, res) => {
134162
exports.postLogoutController = (req, res) => {
135163
res.clearCookie("token").status(200).json({ success: true });
136164
};
165+
166+
exports.getVerifyController = (req, res) => {
167+
const token = req.query.token;
168+
User.findOne({
169+
token: token,
170+
})
171+
.then((users) => {
172+
if (users.active === false) {
173+
users.active = true;
174+
users
175+
.save()
176+
.then((result) => {
177+
return res.status(200).json({
178+
success: true,
179+
msg: "Email verified.",
180+
});
181+
})
182+
.catch((err) => {
183+
console.log(err);
184+
return res.status(505).json({
185+
success: false,
186+
errType: "dberr",
187+
msg: "Internal Server Error.",
188+
});
189+
});
190+
} else {
191+
return res.status(200).json({
192+
success: true,
193+
msg: "Email already verified.",
194+
});
195+
}
196+
})
197+
.catch((err) => {
198+
console.log(err);
199+
return res.status(505).json({
200+
success: false,
201+
errType: "dberr",
202+
msg: "Internal Server Error.",
203+
});
204+
});
205+
};

models/user.model.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const UserSchema = new Schema({
1414
password: {
1515
type: String,
1616
required: true,
17+
},
18+
active: {
19+
type: Boolean,
20+
default: false,
21+
},
22+
token: {
23+
type: String,
24+
required: false,
1725
}
1826
});
1927

package-lock.json

Lines changed: 35 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"dotenv": "^16.0.1",
88
"express": "^4.18.1",
99
"jsonwebtoken": "^8.5.1",
10-
"mongoose": "^6.4.1"
10+
"mongoose": "^6.4.1",
11+
"node-mailer": "^0.1.1"
1112
},
1213
"scripts": {
1314
"start": "nodemon app.js"

routes/auth.routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ router.post('/login', authControllers.postLoginController);
1010

1111
router.post('/logout', authControllers.postLogoutController);
1212

13+
router.get('/verify', authControllers.getVerifyController);
14+
1315
module.exports = router;

0 commit comments

Comments
 (0)