Skip to content

Chris #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/config/auth.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
secret: "bezkoder-secret-key"
secret: "bezkoder-secret-key",
user: "mediumtutorial2021@gmail.com",
pass: "medium2021t",
};
2 changes: 1 addition & 1 deletion app/config/db.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
HOST: "localhost",
HOST: "127.0.0.1",
PORT: 27017,
DB: "bezkoder_db"
};
27 changes: 27 additions & 0 deletions app/config/nodemailer.config.js
Original file line number Diff line number Diff line change
@@ -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: `<h1>Email Confirmation</h1>
<h2>Hello ${name}</h2>
<p>Thank you for subscribing. Please confirm your email by clicking on the following link</p>
<a href='http://localhost:8081/confirm/${confirmationCode}'>Click Here</a>
`
}).catch(err => console.log(err));
};
49 changes: 46 additions & 3 deletions app/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) => {
Expand All @@ -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
);
});
}
);
Expand All @@ -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
);
});
});
}
Expand All @@ -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
Expand Down Expand Up @@ -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));
};
9 changes: 9 additions & 0 deletions app/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions app/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ module.exports = function(app) {
);

app.post("/api/auth/signin", controller.signin);

app.get("/api/auth/confirm/:confirmationCode", controller.verifyUser);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}