primus icon indicating copy to clipboard operation
primus copied to clipboard

Pass res Response to auth function in middleware/authorization

Open yagop opened this issue 10 years ago • 7 comments

I think It would be useful to pass res object in the auth function in the authorization middleware.

https://github.com/primus/primus/blob/62c02cd6236ebdd88568ef2daa88da33a263f5a8/middleware/authorization.js#L21

Maybe as third argument in order to not breaking compatibility.

yagop avatar Mar 04 '16 11:03 yagop

What's your use case?

lpinca avatar Mar 04 '16 20:03 lpinca

I want to set some attributes to res, like res.locals in Express. In my use case, after authorization I want to set a user object.

yagop avatar Mar 05 '16 10:03 yagop

It may sound odd but can't you do that in a new middleware that is added after the authorization middleware?

primus.before('locals', function (req, res) {
  res.locals = { foo: 'bar' };
}, primus.indexOfLayer('authorization') + 1);

The overhead should be minimal.

lpinca avatar Mar 05 '16 10:03 lpinca

To set the user I have to call the same API to verify the authorization. Its much better having res in auth function.

yagop avatar Mar 05 '16 10:03 yagop

This is a bit messy but in the auth middleware you can set you stuff on the req object and in the next middleware move it to the res object.

lpinca avatar Mar 05 '16 10:03 lpinca

I would better write an authorization middleware which do the same as the authorization (close connection if not valid) and write to res my values.

Why so against about passing res to auth function?

yagop avatar Mar 05 '16 11:03 yagop

I see no real value, I mean, I understand your use case, but ideally the auth function should only validate and do stuff on the incoming connection. This is more a logical condition than a hard requirement but this is how I picture it. Anyway let's keep this open an wait for others to weigh in.

lpinca avatar Mar 05 '16 11:03 lpinca