Local strategy: ctx.state.user is undefined
See original GitHub issueI am trying to set up local authentication for my react app. Since I would like to handle the routing on the client side, I am not redirecting via passport. My flow would look like this:
- POST request from the client with email and password
 - GET request from the client to fetch user data
 
I have set it up like this:
router.post('/login/local', passport.authenticate('local-login', function(err, user, info) {
    if (err) // handle error
    else if (!user && info) // handle no user
    else {
      // handle successful login
      ctx.login(user);
    }
  }));
That works fine and I send back the response to the client. But when I send another request from the client to access the user data, there is no user data attached to the request:
router.get('/profile', (ctx) => {
    // ctx.state.user is undefined
}
What am I missing here? Is this not how it should work?
Issue Analytics
- State:
 - Created 6 years ago
 - Reactions:2
 - Comments:7 (4 by maintainers)
 
Top Results From Across the Web
passport.js - Why is ctx.state.user undefined after logging in ...
I am using koa-passport to handle the local signup/login in my react ... router.get('/profile', (ctx) => { // ctx.state.user is undefined }.
Read more >Ctx.state.user undefined in context policy custom plugin
Ctx.state.user undefined in context policy custom plugin ... So I cannot get the user in the policy context. Based on the example on...
Read more >Controller context does not contain user object (ctx.state.user)
When I reload browser tab with F5, ctx.state.user is undefined . When I'm using AJAX-request, ctx.state.user is filled as expected.
Read more >koa.Context JavaScript and Node.js code examples - Tabnine
statusCode; ctx.response.body = body === undefined ? ... this enables local development with multiple users / sessions. strategies.unshift("local"); } await ...
Read more >Koa - next generation web framework for node.js
Introduction. Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes! That’s exactly it. I had to allow sending cookies on the fetch and in koa-cors. Thank you so much, really appreciate it.
Since you are using the CORS middleware, does the GET request for fetching the user information come from another host? If so, you may need to adjust the
credentialsoption of thefetchAPI.