orszaczky
orszaczky

Reputation: 15645

routing in mean.js (how to include middlware?)

I'm trying to include a middleware (passport-http-bearer) in MEAN.js, however it uses a different routing syntax than Express 4.

Express API sytnax is:

app.get('/', function(req, res){
  res.send('hello world');
});

In MEAN.js routes are defined like this:

app.route('/articles')
    .get(articles.list)
    .post(users.requiresLogin, articles.create);

How do I include a middleware in the MEAN.js router (in my case passport-http-bearer to check for a token)?

http-bearer's example implementation as middleware is:

app.get('/profile', 
  passport.authenticate('bearer', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

How should I do this in MEAN.js?

Upvotes: 0

Views: 877

Answers (1)

orszaczky
orszaczky

Reputation: 15645

For anyone else ending up here trying to figure out how to do this, here is how it can be done:

app.route('/articles')
    .get(passport.authenticate('bearer', { session: false }), articles.list)
    .post(passport.authenticate('bearer', { session: false }), articles.create);

Or to make it look nicer, the whole auth function could be put in users.authorization.server.controller.js and called woith something like this:

app.route('/articles')
    .get(users.requiresToken, articles.list)
    .post(users.requiresToken, articles.create);

Upvotes: 2

Related Questions