0

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?

orszaczky
  • 13,301
  • 8
  • 47
  • 54
  • 2
    That sure does look like Express 4 to me... http://expressjs.com/4x/api.html#router.route – Brad Sep 03 '14 at 16:40

1 Answers1

2

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);
orszaczky
  • 13,301
  • 8
  • 47
  • 54
  • How you did that? How did you manage authentication itself? Do you generate token? How you update them? Can you share repo on github? Couldn't integrate `passport-http-bearer` =( – Medet Tleukabiluly May 28 '15 at 08:39