question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Local strategy: ctx.state.user is undefined

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JonathanWbncommented, Apr 18, 2017

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.

1reaction
rkusacommented, Apr 18, 2017

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 credentials option of the fetch API.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found