diff --git a/app/config/auth.config.js b/app/config/auth.config.js index 4819df3..9396a6d 100644 --- a/app/config/auth.config.js +++ b/app/config/auth.config.js @@ -1,3 +1,5 @@ module.exports = { - secret: "bezkoder-secret-key" + secret: "bezkoder-secret-key", + user: "mediumtutorial2021@gmail.com", + pass: "medium2021t", }; diff --git a/app/config/db.config.js b/app/config/db.config.js index ac4ae86..1781845 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -1,5 +1,5 @@ module.exports = { - HOST: "localhost", + HOST: "127.0.0.1", PORT: 27017, DB: "bezkoder_db" }; \ No newline at end of file diff --git a/app/config/nodemailer.config.js b/app/config/nodemailer.config.js new file mode 100644 index 0000000..32bd280 --- /dev/null +++ b/app/config/nodemailer.config.js @@ -0,0 +1,27 @@ +const nodemailer = require("nodemailer"); +const config = require("../config/auth.config"); + +const user = config.user; +const pass = config.pass; + +const transport = nodemailer.createTransport({ + service: "Gmail", + auth: { + user: user, + pass: pass, + }, +}); + +module.exports.sendConfirmationEmail = (name, email, confirmationCode) => { + console.log("Check"); + transport.sendMail({ + from: user, + to: email, + subject: "Please confirm your account", + html: `
Thank you for subscribing. Please confirm your email by clicking on the following link
+ Click Here + ` + }).catch(err => console.log(err)); +}; \ No newline at end of file diff --git a/app/controllers/auth.controller.js b/app/controllers/auth.controller.js index d455114..57a2b60 100644 --- a/app/controllers/auth.controller.js +++ b/app/controllers/auth.controller.js @@ -1,4 +1,5 @@ const config = require("../config/auth.config"); +const nodemailer = require("../config/nodemailer.config"); const db = require("../models"); const User = db.user; const Role = db.role; @@ -7,10 +8,14 @@ var jwt = require("jsonwebtoken"); var bcrypt = require("bcryptjs"); exports.signup = (req, res) => { + + const token = jwt.sign({email: req.body.email}, config.secret); + const user = new User({ username: req.body.username, email: req.body.email, - password: bcrypt.hashSync(req.body.password, 8) + password: bcrypt.hashSync(req.body.password, 8), + confirmationCode: token }); user.save((err, user) => { @@ -37,7 +42,13 @@ exports.signup = (req, res) => { return; } - res.send({ message: "User was registered successfully!" }); + res.send({ message: "User was registered successfully! Please check your email." }); + + nodemailer.sendConfirmationEmail( + user.username, + user.email, + user.confirmationCode + ); }); } ); @@ -55,7 +66,13 @@ exports.signup = (req, res) => { return; } - res.send({ message: "User was registered successfully!" }); + res.send({ message: "User was registered successfully! Please check your email." }); + + nodemailer.sendConfirmationEmail( + user.username, + user.email, + user.confirmationCode + ); }); }); } @@ -77,6 +94,12 @@ exports.signin = (req, res) => { return res.status(404).send({ message: "User Not found." }); } + if (user.status != "Active") { + return res.status(401).send({ + message: "Pending Account. Please verify your Email!" + }); + } + var passwordIsValid = bcrypt.compareSync( req.body.password, user.password @@ -107,3 +130,23 @@ exports.signin = (req, res) => { }); }); }; + +exports.verifyUser = (req, res, next) => { + User.findOne({ + confirmationCode: req.params.confirmationCode, + }) + .then((user) => { + if (!user) { + return res.status(404).send({ message: "User Not Found." }); + } + + user.status = "Active"; + user.save((err) => { + if (err) { + res.status(500).send({message: err}); + return; + } + }); + }) + .catch((e) => console.log("error", e)); +}; diff --git a/app/models/user.model.js b/app/models/user.model.js index 03af285..6f1ce1a 100644 --- a/app/models/user.model.js +++ b/app/models/user.model.js @@ -6,6 +6,15 @@ const User = mongoose.model( username: String, email: String, password: String, + status: { + type: String, + enum: ['Pending', 'Active'], + default: 'Pending' + }, + confirmationCode: { + type: String, + unique: true + }, roles: [ { type: mongoose.Schema.Types.ObjectId, diff --git a/app/routes/auth.routes.js b/app/routes/auth.routes.js index dcfb044..ff7d66c 100644 --- a/app/routes/auth.routes.js +++ b/app/routes/auth.routes.js @@ -20,4 +20,6 @@ module.exports = function(app) { ); app.post("/api/auth/signin", controller.signin); + + app.get("/api/auth/confirm/:confirmationCode", controller.verifyUser); }; diff --git a/package.json b/package.json index b778f3a..18833ac 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "cors": "^2.8.5", "express": "^4.17.1", "jsonwebtoken": "^8.5.1", - "mongoose": "^5.9.1" + "mongoose": "^5.9.1", + "nodemailer": "^6.9.0" } }