Initial commit
This commit is contained in:
23
lib/jwtValidator.js
Normal file
23
lib/jwtValidator.js
Normal file
@@ -0,0 +1,23 @@
|
||||
'user strict';
|
||||
const { ROOT_PATH, API_CONFIG } = process.env;
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const apiConfig = require(ROOT_PATH + API_CONFIG);
|
||||
const secret = apiConfig.authentication.jwtSecret;
|
||||
|
||||
function middleware( req, res, next ){
|
||||
if( req.JWT ){
|
||||
req.JWT.payload = jwt.verify( req.JWT.raw, apiConfig.authentication.jwtSecret , (err, user) => {
|
||||
if( err ){
|
||||
return res.status(401).send({error:"Unauthorized",code:401});
|
||||
}
|
||||
});
|
||||
next();
|
||||
}else{
|
||||
return res.status(401).send({error:"Unauthorized",code:401});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
middleware
|
||||
};
|
||||
Reference in New Issue
Block a user