I would like to add user login authentication for my REST API server which was implemented in node.js restify.
I intend to use this module restify-ensure-login
.
https://www.npmjs.com/package/restify-ensure-login
I want all the API functions to require user login authentication. Am I right to say that every API must contain this line ensureLoggedIn('/login'),
?
Something like below?
app.get('/settings',
ensureLoggedIn('/login'),
function(req, res) {
res.render('settings', { user: req.user });
});
Is there some way to have this line ensureLoggedIn('/login'),
appear once only?
This is a classic use-case for middleware:
// Runs before every downstream route
app.use(ensureLoggedIn('/login'));
app.get('/settings', function(req, res) {
res.render('settings', { user: req.user });
});