Currently, I am using Express/Node and have developed authentication middleware to validate JWT on each request.
My requirement is to disable this middleware for a specific route (POST '/api/user/') while keeping it active for another route (GET '/api/user/').
Is there a way to accomplish this?
Below is the relevant code snippet:
app.js
// app.js
app.use('/api/userauth', require('./Controllers/api/userauth.js'))
app.use(require('./Middleware/Authenticate.js'));
app.use('/api/user', require('./Controllers/Api/User.js'));
app.use('/api/item', require('./Controllers/Api/Item.js'));
authenticate.js
//authenticate.js middleware
var token = req.body.token || req.query.token || req.headers['x-access-token'];
if (token) {
jwt.verify(token, secretKey.secretKey, function(err, decoded){
if (err) {
return res.json({
success : false,
message : "failed to auth token."
})
} else {
console.log(decoded);
req.decoded = decoded;
next();
}
})
} else {
res.status(403).send({
success:false,
message:'no token provided'
})
}
}
api/user.js
router.route('/')
.get(function(req,res){
models.User.findAll({
}).then(function(users){
res.json(users);
})
})
.post(function(req,res){
models.User.create({
username : req.body.username,
password : req.body.password,
firstname : req.body.firstname,
lastname : req.body.lastname
}).then(function(user){
res.json({
"Message" : "Succesfully created user: ",
"User: " : user
});
});
});
module.exports = router;