Skip to content

Commit

Permalink
Resolved merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
vpul committed May 24, 2019
2 parents 7b588a5 + e5c092a commit d5a1937
Show file tree
Hide file tree
Showing 11 changed files with 1,251 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ typings/
.next

secretKey.js

config
41 changes: 26 additions & 15 deletions controller/emailVerify.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
const nodemailer = require('nodemailer')

exports.verifyEmail = async (email,host,token)=>{
let transporter = nodemailer.createTransport({ service: process.env.GMAIL_SERVICE,
port: process.env.PORT,
secure :false,
auth:{ user: process.env.GMAIL_USER, pass: process.env.GMAIL_PASSWORD } });
let mailOptions = {
from : '[email protected]',
to: email,
subject: 'Account Verification Token',
text: 'Hello,\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/' + host + '\/mentors\/confirmation/?token=' + token + '.\n' };
await transporter.sendMail(mailOptions,(err)=> {
if (err) {
return console.log(err)
}
const path = require('path');
var EmailTemplate = require('email-templates');
exports.verifyEmail = async (email, host, token) => {
let transporter = nodemailer.createTransport({
service: process.env.GMAIL_SERVICE,
port: process.env.PORT,
secure: false,
auth: { user: process.env.GMAIL_USER, pass: process.env.GMAIL_PASSWORD }
});
const emails = new EmailTemplate({
send: true,
message: {
from: process.env.GMAIL_USER
},
transport: transporter,
preview: false
});
emails.send({
template: path.join(__dirname, '..', 'email', 'mentors'),
message: {
to: email
},
locals: {
host: host,
token: token
}
})
}
65 changes: 0 additions & 65 deletions controller/loginController.js

This file was deleted.

58 changes: 35 additions & 23 deletions controller/signUpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,39 @@ exports.create = async (req, res, next) => {
if (!errors.isEmpty()) {
return res.status(http.UNPROCESSABLE_ENTITY).json({ errors: errors.array() });
}
let role = null
if(req.body.user_role === 1) role = "Mentor"
else role ="student"
let hash = await bcrypt.hash(req.body.password, SALTING)
const token = await jwt.sign({ email: req.body.email }, secretKey.token.key, { expiresIn: process.env.access_token_exp })
const access_token = await jwt.sign({ email: req.body.email }, secretKey.token.key, { expiresIn: process.env.access_token_exp })
const refresh_token = await jwt.sign({ email: req.body.email }, secretKey.token.key, { expiresIn: process.env.refresh_token_exp })
const user = new User({
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
user_role: role,
password: hash,
refresh_token: refresh_token
})
user.save()
host = req.get('host')
await email_verify.verifyEmail(user.email, host, token)
try {
await user.save()
host = req.get('host')
await email_verify.verifyEmail(user.email, host, access_token)
}
catch (err) {
console.log(err)
if (err) { return res.status(http.INTERNAL_SERVER_ERROR).send({ msg: http.getStatusText(http.INTERNAL_SERVER_ERROR) }); }
}
const response = {
"status": statusMsg.success.msg,
"accessToken": token,
"accessToken": access_token,
"data": user,
"message": statusMsg.email.msg + user.email + '.'
}
res.status(http.CREATED).json({ response })

}

exports.confirmation = async(req, res) => {
exports.confirmation = async (req, res) => {
if (req.query.token) {
try {
decoded = await jwt.verify(req.query.token, secretKey.token.key);
Expand All @@ -50,24 +59,27 @@ exports.confirmation = async(req, res) => {
return res.status(http.UNAUTHORIZED).json({ message: http.getStatusText(http.UNAUTHORIZED) });
}
const email = decoded.email;
await User.findOne({ email: email }, (err, user) => {
if (!user) return res.status(http.BAD_REQUEST).send({ status: statusMsg.fail.msg, msg: http.getStatusText(http.BAD_REQUEST) });
if (user.verified_email) return res.status(http.CONFLICT).send({ status: statusMsg.fail.msg, msg: http.getStatusText(http.CONFLICT) });
user.verified_email = true
user.save(function (err) {
if (err) { return res.status(http.INTERNAL_SERVER_ERROR).send({ msg: http.getStatusText(http.INTERNAL_SERVER_ERROR) }); }
res.status(http.OK).send({ status: statusMsg.success.msg, message: http.getStatusText(http.OK) });
})
})
let user = await User.findOne({ email: email })
if (!user) return res.status(http.BAD_REQUEST).send({ status: statusMsg.fail.msg, msg: http.getStatusText(http.BAD_REQUEST) });
if (user.verified_email) return res.status(http.CONFLICT).send({ status: statusMsg.fail.msg, msg: http.getStatusText(http.CONFLICT) });
user.verified_email = true
try {
await user.save()
}
catch (err) {
if (err) { return res.status(http.INTERNAL_SERVER_ERROR).send({ msg: http.getStatusText(http.INTERNAL_SERVER_ERROR) }); }
}
console.log(user)
res.status(http.OK).send({ status: statusMsg.success.msg, message: http.getStatusText(http.OK) });
}
}

exports.resendToken = async (req, res) => {
await User.findOne({ email: req.body.email }, async (err, user) => {
if (!user) return res.status(http.NOT_FOUND).json({ status: statusMsg.fail.msg, msg: http.getStatusText(http.NOT_FOUND) });
if (user.verified_email) return res.status(http.BAD_REQUEST).json({ status: statusMsg.fail.msg, msg: http.getStatusText(http.BAD_REQUEST) });
const token = await jwt.sign({ email: req.body.email }, secretKey.token.key, { expiresIn: process.env.access_token_exp })
host = req.get('host');
await email_verify.verifyEmail(user.email, host, token)
})
let user = await User.findOne({ email: req.body.email })
if (!user) return res.status(http.NOT_FOUND).json({ status: statusMsg.fail.msg, msg: http.getStatusText(http.NOT_FOUND) });
if (user.verified_email) return res.status(http.BAD_REQUEST).json({ status: statusMsg.fail.msg, msg: http.getStatusText(http.BAD_REQUEST) });
const token = await jwt.sign({ email: req.body.email }, secretKey.token.key, { expiresIn: process.env.access_token_exp })
host = req.get('host');
await email_verify.verifyEmail(user.email, host, token)

}
3 changes: 2 additions & 1 deletion views/email.pug → email/mentors/html.pug
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
doctype html
html
body
p Hello, Please verify your account by clicking the link: http:\\' + #{host} + '\mentors\confirmation/?token=' + #{token}.
p Hello, Please verify your account by clicking the link:
a(href = `http://${host}/mentors/confirmation/?token=${token}`) Verify
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const socialAuth = require('../mentors/routes/socialAuth')
const cors = require('cors')
const morgan = require('morgan')

dotenv.config({
path: './config/variables.env'
Expand All @@ -17,10 +19,13 @@ mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true)
app.listen(3000, () =>
console.log('Hello Mentors'),
);
app.get('/',(req,res)=>{
res.json({msg: "HELLO MENTORS"})})

)
app.get('/', (req, res) => {
res.json({ msg: "HELLO MENTORS" })
})

app.use(bodyParser.json())
app.use(cors())
app.use(morgan('combined'))
app.use('/mentors/', signup)
app.use('/auth', socialAuth)
4 changes: 2 additions & 2 deletions middleware/jwtValidation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports.verifyToken = (req,res,next) => {
exports.verifyToken = (req, res, next) => {
const bearerHeader = req.headers['authorization'];
if(typeof bearerHeader !== 'undefined') {
if (typeof bearerHeader !== 'undefined') {
const bearer = bearerHeader.split(' ');
const bearerToken = bearer[1];
req.token = bearerToken;
Expand Down
32 changes: 23 additions & 9 deletions middleware/signupValidation.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
const { body } = require('express-validator/check')
const User = require('../models/user')

exports.validate = (method) => {
switch (method) {
case 'createUser':
return [
body('email', 'Email already exists').exists(),
case 'createUser':
return [
body('email').custom(async (value, { req }) => {
let user = await User.findOne({ email: req.body.email })
if (user) {
throw new Error('Email already exists');
}
return true;
}),
body('email', 'Invalid email').isEmail(),
body('first_name','First name should not be empty').not().isEmpty(),
body('last_name','Last name should not be empty').not().isEmpty(),
body('password','Password name should not be empty').not().isEmpty(),
body('password').custom((value , { req }) => {
if (value !== req.body.confirm_password) {
body('first_name', 'First name should not be empty').not().isEmpty(),
body('last_name', 'Last name should not be empty').not().isEmpty(),
body('password', 'Password name should not be empty').not().isEmpty(),

This comment has been minimized.

Copy link
@shakyasaijal

shakyasaijal May 25, 2019

Contributor

name?

body('password').custom((value, { req }) => {
if (value !== req.body.confirm_password) {
throw new Error('Password confirmation is incorrect');
}
return true;
}),
body('password','Invalid Password').matches('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})')
body('password', 'Invalid Password').matches('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})'),
// body('password', 'Invalid Password').custom((value, { req }) => {
// if (body('password').contains(req.body.first_name) ){
// throw new Error('Password should not contains name/email');
// }
// return true;
// }),
body('user_role', 'Invalid user role').isBoolean()
]
}
}
Loading

0 comments on commit d5a1937

Please sign in to comment.