-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.js
39 lines (33 loc) · 1.08 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
39
const Joi = require('joi');
const jwt = require('jsonwebtoken');
// validating registration
const registerValidation = (data) => {
const schema = Joi.object({
name: 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);
}
// validating login
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);
}
// logic to verify our token (JWT)
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 };