I have many routes. Most of them require authentication. One doesn't.
Here they are:
router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})
router.get('/:id', function (req,res) {...})
1. Let's imagine I didn't have the public route.
At the top of the page I could just put a security check middleware, and all is well. It will only let through secure connections, and will redirect non secure.
router.use(function (req,res,next) {
securityCheck()
next()
})
router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})
router.get('/:id', function (req,res) {...})
This would work. this makes all the secure routes secure, but it blocks me from the public route ('/:id').
2. I could move the public route to the top:
router.get('/:id', function (req,res) {...})
router.use(function (req,res,next) {
securityCheck()
next()
})
router.get('/secure1', function (req,res) {...})
router.get('/secure2', function (req,res) {...})
router.get('/secure3', function (req,res) {...})
But this way it catches all of my requests and all the secure paths are inaccessible.
3. I could put a middleware on every single secure route, but that seems a little tedious and prone to human-errors:
router.get('/secure1',securityCheck(), function (req,res) {...})
So, is there a better option I didn't consider? What is considered the best practice?
Thank you