- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthMiddleware.js
More file actions
Latest commit
21 lines (17 loc) · 576 Bytes
/
Copy pathauthMiddleware.js
File metadata and controls
21 lines (17 loc) · 576 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import{verifyAccessToken}from'../utils/jwt.js'
importAppErrorfrom'../utils/AppError.js'
import{ERROR_CODES}from'../constants/errorCodes.js'
constauthMiddleware=(req,res,next)=>{
consttoken=req.cookies.accessToken
if(!token){
thrownewAppError('Unauthorized',401,ERROR_CODES.UNAUTHORIZED)
}
try{
constdecoded=verifyAccessToken(token)
req.user=decoded
next()
}catch(err){
thrownewAppError('Invalid token',401,ERROR_CODES.INVALID_TOKEN)
}
}
exportdefaultauthMiddleware