Skip to content

Commit 2427807

Browse files
committed
control email format, hash password on update and checked email is always unique
1 parent 4ad1bed commit 2427807

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

api/controllers/employees.controller.js

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const bcrypt = require('bcrypt')
22
const jwt = require('jsonwebtoken')
3+
const { isValidEmail } = require('../../utils')
34

45
const { employeeModel } = require('../models/employees.model')
56

@@ -11,29 +12,33 @@ exports.addEmployee = (req, res) => {
1112
if (user) {
1213
res.status(409).json({ err: 'Email already exists. Try another one' })
1314
} else {
14-
employeeModel
15-
.create({
16-
firstName: req.body.firstName,
17-
lastName: req.body.lastName,
18-
specialty: req.body.specialty,
19-
rol: req.body.rol,
20-
email: req.body.email,
21-
password: hashed_pwd
22-
})
23-
.then(user => {
24-
const user_data = { rol: user.rol, email: user.email }
15+
if (isValidEmail(req.body.email)) {
16+
employeeModel
17+
.create({
18+
firstName: req.body.firstName,
19+
lastName: req.body.lastName,
20+
specialty: req.body.specialty,
21+
rol: req.body.rol,
22+
email: req.body.email,
23+
password: hashed_pwd
24+
})
25+
.then(user => {
26+
const user_data = { rol: user.rol, email: user.email }
2527

26-
const token = jwt.sign(
27-
user_data,
28-
process.env.SECRET, // TODO SECRET MORE SECRET PLEASE
29-
{ expiresIn: '1h' }
30-
)
31-
return res.json({ token: token, ...user_data })
32-
})
33-
.catch(err => {
34-
console.log(err)
35-
res.status(500).json({ msg: 'Error' })
36-
})
28+
const token = jwt.sign(
29+
user_data,
30+
process.env.SECRET, // TODO SECRET MORE SECRET PLEASE
31+
{ expiresIn: '1h' }
32+
)
33+
return res.json({ token: token, ...user_data })
34+
})
35+
.catch(err => {
36+
console.log(err)
37+
res.status(500).json({ msg: 'Error' })
38+
})
39+
} else {
40+
res.status(409).json({ err: 'Wrong email format' })
41+
}
3742
}
3843
})
3944
.catch(err => {
@@ -57,30 +62,27 @@ exports.updateEmployee = (req, res) => {
5762
.findById(req.params.idEmployee)
5863
.then(user => {
5964
if (req.body.employee.email) {
60-
employeeModel
61-
.findOne({ email: req.body.employee.email })
62-
.then(user => {
63-
if (user) res.status(403).json({ msg: 'The email already exists!' })
64-
})
65-
.catch(err => {
66-
console.log(err)
67-
res.status(500).json({ msg: 'Error' })
68-
})
65+
if (!isValidEmail(req.body.employee.email)) {
66+
return res.status(409).json({ err: 'Wrong email format' })
67+
}
6968
}
7069
if (req.body.employee.password) {
71-
req.body.employee.password = bcrypt.hashSync(req.body.employee.password, 10)
70+
user.password = bcrypt.hashSync(req.body.employee.password, 10)
7271
}
7372

74-
console.log(result)
73+
user.firstName = req.body.employee.firstName ?? user.firstName
74+
user.lastName = req.body.employee.lastName ?? user.lastName
75+
user.specialty = req.body.employee.specialty ?? user.specialty
76+
user.email = req.body.employee.email ?? user.email
77+
user.rol = req.body.employee.rol ?? user.rol
78+
7579
user.save(function (err, result) {
7680
if (err) {
77-
console.log(err);
78-
}
79-
else {
80-
console.log(result)
81+
res.status(500).json({ msg: 'Error' })
82+
} else {
83+
res.status(200).json({ msg: 'Update successful!' })
8184
}
8285
})
83-
res.status(200).json({ msg: 'Update successful!' })
8486
})
8587
.catch(err => {
8688
console.log(err)

utils/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ exports.checkCustomerServiceOrManager = (req, res, next) => {
7272
res.json({ err: 'Token not valid' })
7373
}
7474
})
75-
}
75+
}
76+
77+
exports.isValidEmail = (email) => {
78+
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
79+
return re.test(String(email).toLowerCase())
80+
}

0 commit comments

Comments
 (0)