-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.js
38 lines (30 loc) · 1.03 KB
/
validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const Joi = require('joi');
const registerValidation = (data) => {
const schema = Joi.object({
username: Joi.string().min(6).max(255).required(),
email: Joi.string().min(6).max(255).required(),
password: Joi.string().min(6).max(255).required()
});
return schema.validate(data);
}
const loginValidation = (data) => {
const schema = Joi.object({
email: Joi.string().min(6).max(255).required(),
password: Joi.string().min(6).max(255).required()
});
return schema.validate(data);
}
const verifyToken = (req, res, next) => {
const token = req.header('auth-token');
if (!token) {
return res.status(401).json({ error: 'Access denied!' });
}
try {
const verified = jwt.verify(token, process.env.TOKEN_SECRET);
req.user = verified;
next();
} catch (error) {
res.status(400).json({ error: 'Token is not valid!'} );
}
}
module.exports = { registerValidation, loginValidation, verifyToken };