Nuxt.js: Programmatic usage of nuxt with express serverMiddleware as API doesn't share or update req.session

Created on 26 Oct 2018  ·  4Comments  ·  Source: nuxt/nuxt.js

Version

2.1

Reproduction link

https://google.com

Steps to reproduce

Hi, I am using nuxt programmatically and using serverMiddleware with it's own express instance which modifies req.user on some api endpoints.

However, when I navigate to different routes after mutating req.user, nuxt is overriding req.user with one from an earlier state. I believe it is because the serverMiddleware version and nuxt are using different instances of connect under the hood.

Has anyone else experienced this, and how have you dealt with it?

I can confirm the connect.sid is the same for both the nuxt routes and api routes.

Thank you.

What is expected ?

req.user being mutated in api and nuxt routes.

What is actually happening?

req.user is not mutated in nuxt routes.

This bug report is available on Nuxt community (#c8046)
bug-report stale

Most helpful comment

Here are steps to repro, adding a few route handlers to the auth example from https://nuxtjs.org/examples/auth-routes/:

app.get('*', function (req, res,next) {
  if (!req.session.user) req.session.user = "jimmy"
   next()
})
app.get('/mutate', function (req, res) {
  req.session.user = "bob"
  res.send(200)
})
app.get('/user', function (req, res) {
  res.send(req.session.user)
})
  1. Navigate to a nuxt route, like /page/123.
  2. Navigate to /user // outputs "jimmy"
  3. Navigate to /mutate // OK
  4. Navigate to /user // outputs "bob" — good!
  5. Navigate to /page/123
  6. Navigate to /user // outputs "jimmy" — ???

So it seems as though nuxt is not picking up the changes to req.session that are happening.

All 4 comments

Here are steps to repro, adding a few route handlers to the auth example from https://nuxtjs.org/examples/auth-routes/:

app.get('*', function (req, res,next) {
  if (!req.session.user) req.session.user = "jimmy"
   next()
})
app.get('/mutate', function (req, res) {
  req.session.user = "bob"
  res.send(200)
})
app.get('/user', function (req, res) {
  res.send(req.session.user)
})
  1. Navigate to a nuxt route, like /page/123.
  2. Navigate to /user // outputs "jimmy"
  3. Navigate to /mutate // OK
  4. Navigate to /user // outputs "bob" — good!
  5. Navigate to /page/123
  6. Navigate to /user // outputs "jimmy" — ???

So it seems as though nuxt is not picking up the changes to req.session that are happening.

@nikkwong Can you add a Codesandbox please (just fork the referred example and add your changes)?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
  2. Comment the steps to reproduce it

Issues that are labeled as 🕐Pending will not be automatically marked as stale.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  ·  3Comments

pehbehbeh picture pehbehbeh  ·  3Comments

danieloprado picture danieloprado  ·  3Comments

vadimsg picture vadimsg  ·  3Comments

jaredreich picture jaredreich  ·  3Comments