0

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?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295

1 Answers1

1

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 });
});

http://restify.com/#common-handlers-serveruse

danneu
  • 9,244
  • 3
  • 35
  • 63