Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ctx.state to pass user #57

Closed
gingerich opened this issue Jul 10, 2016 · 10 comments
Closed

Support ctx.state to pass user #57

gingerich opened this issue Jul 10, 2016 · 10 comments

Comments

@gingerich
Copy link
Contributor

I believe that best practice for koa v2 would be to store the authenticated user in ctx.state, rather than ctx.request.

Documentation for koa v2 indicates that ctx.state is "the recommended namespace for passing information through middleware ..."
https://github.com/koajs/koa/blob/v2.x/docs/api/context.md#ctxstate

@gingerich gingerich changed the title Are there plans to support ctx.state to pass user? Support ctx.state to pass user Jul 10, 2016
@rkusa
Copy link
Owner

rkusa commented Jul 11, 2016

Sounds good to me. What do you think, should we still keep it in ctx.request with a deprecation warning for now?

@gingerich
Copy link
Contributor Author

gingerich commented Jul 11, 2016

Yeah I think that makes sense. I can put together a PR for this if you want

@rkusa
Copy link
Owner

rkusa commented Jul 11, 2016

A PR is of course highly welcome, but I am also totally fine with implementing it myself.

@gingerich
Copy link
Contributor Author

I'd love to contribute. I'll take a stab at it and see what you think.

@rkusa
Copy link
Owner

rkusa commented Jul 13, 2016

Before I publish a new version I just like to get some feedback on the methods .login(), .logout(), .isAuthenticated() and .isUnauthenticated(). They are currently added to both ctx and ctx.req. I would also like to clean this up a little bit. What is your opinion for the best location where to put them, ctx or also ctx.state?

@gingerich
Copy link
Contributor Author

I tend to think of ctx.state as a simple data store, where logic does not belong, so my preference would be for ctx.
However, .isAuthenticated() and .isUnauthenticated() pertain more to a specific request (depending whether sessions are used), so I could see an argument for adding those two to ctx.req.
It's hard to say what's best, but if I had to choose one place for all methods, I think ctx makes the most sense.

@rkusa
Copy link
Owner

rkusa commented Jul 15, 2016

Thanks for your feedback! I agree on your points. I think .isAuthenticated() and .isUnauthenticated() fit into both ctx and ctx.request (request should probably preferred over req). But since having it both does not make too much sense, I will probably go with ctx.

@rkusa rkusa closed this as completed Jul 15, 2016
@gingerich
Copy link
Contributor Author

Before you publish, I just discovered a problem with my implementation that could create a race condition.

Basically, next() can be called before ctx.state.user is assigned because resolve callback is invoked first.

I'll fix in a new PR

@jeffijoe
Copy link

jeffijoe commented Jul 31, 2016

It appears that the verify callback in strategies use the req setter. E.g. https://github.com/jaredhanson/passport-http/blob/master/lib/passport-http/strategies/basic.js#L95

I checked the stack trace:

at IncomingMessage.Object.defineProperty.set [as user] (\node_modules\koa-passport\lib\framework\koa.js:37:19)
at Object.properties.(anonymous function).set (\node_modules\koa-passport\lib\framework\request.js:113:16)
at Object.req.login.req.logIn (\node_modules\passport\lib\http\request.js:44:18)
at Object.<anonymous> (\node_modules\koa-passport\lib\framework\request.js:104:27)
at BasicStrategy.strategy.success (\node_modules\passport\lib\middleware\authenticate.js:235:13)
at verified (\node_modules\passport-http\lib\passport-http\strategies\basic.js:91:10)

So this deprecation notice is unavoidable it seems.

Should I open a new issue?

@rkusa
Copy link
Owner

rkusa commented Aug 1, 2016

Thanks for reporting, I've created an issue for this: #66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants