-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
40 lines (35 loc) · 1010 Bytes
/
util.js
File metadata and controls
40 lines (35 loc) · 1010 Bytes
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
40
const jwt = require('jsonwebtoken')
const getToken = (user) =>{
const signinUser={
_id: user._id,
name: user.name,
email: user.email,
isAdmin: user.isAdmin,
}
return jwt.sign(signinUser, process.env.JWT_SECRET,{
expiresIn: '48h'
})
}
const isAuth = (req, res, next) => {
const token = req.headers.authorization
if(token){
const onlyToken = token.slice(6, token.length)
jwt.verify(onlyToken, process.env.JWT_SECRET, (err, decode)=>{
if(err){
return res.status(401).send({msg: 'Invalid token'})
}
req.user = decode
return next()
})
}else{
return res.status(401).send({msg: 'Token is not supplied'})
}
}
const isAdmin = (req, res, next) => {
if(req.user && req.user.isAdmin){
return next()
}else{
return res.status(401).send({msg: 'Not an admin'})
}
}
module.exports = { getToken, isAuth, isAdmin }