Skip to content

Commit

Permalink
fixes: user_role addded
Browse files Browse the repository at this point in the history
  • Loading branch information
AwaleRohin committed May 23, 2019
1 parent 9bbdc7d commit 20b321f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ typings/
# next.js build output
.next

secretKey.js
secretKey.js

config
4 changes: 4 additions & 0 deletions controller/signUpController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ 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 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
})
Expand Down
18 changes: 16 additions & 2 deletions middleware/signupValidation.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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(),
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(),
Expand All @@ -15,7 +22,14 @@ exports.validate = (method) => {
}
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, req.body.last_name,req.body.email.substr(0,req.body.email.indexOf('@')))) {
throw new Error('Password should not contains name/email');
}
return true;
}),
body('user_role', 'Invalid user role').isBoolean()
]
}
}
20 changes: 10 additions & 10 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const mongoose = require('mongoose')
var schema = mongoose.Schema({
first_name: {type:String, required:true},
last_name :{type:String,required:true},
email: {type:String, required:true, unique:true},
password : {type:String, required:true},
dob : {type:Date},
phone : {type:Number},
address : {type:String},
user_role : {type:String},
verified_email : {type: Boolean, default: false},
refresh_token : {type:Array,required:true},
first_name: { type: String, required: true },
last_name: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
dob: { type: Date },
phone: { type: Number },
address: { type: String },
user_role: { type: String, required: true },
verified_email: { type: Boolean, default: false },
refresh_token: { type: Array, required: true },
})
schema.methods.toJSON = function () {
var obj = this.toObject()
Expand Down

0 comments on commit 20b321f

Please sign in to comment.