-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthMiddleware.js
More file actions
30 lines (29 loc) · 900 Bytes
/
Copy pathauthMiddleware.js
File metadata and controls
30 lines (29 loc) · 900 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
const jwt = require('jsonwebtoken')
const JWT_SECRET = process.env.JWT_SECRET
require('dotenv').config();
function authMiddleware(req,res,next){
const authHeader = req.headers["authorization"];
console.log("authheader here:" + authHeader +"ends here")
const token = authHeader.split(" ")[1];//this part removes the bearer part from the token and gives us the actual token
console.log("token here:" + token +"ends here")
if(!token){
return res.status(401).json({
msg : 'token not found'
})
}else{
const decode=jwt.verify(token,JWT_SECRET)
const user_Id = decode.user_Id
if(!user_Id){
res.json({
msg : 'user not found'
})
return
}else{
req.user_Id = user_Id;
next()
}
}
}
module.exports = {
authMiddleware : authMiddleware
}