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

Setting session before login throws Error: Can’t set headers after they are sent #89

Closed
Yuripetusko opened this issue Jul 19, 2017 · 7 comments

Comments

@Yuripetusko
Copy link

I need to have sessionId even before user logs in, to tracks browsing session of user and tie him to it later when he logs in, so I did this:

server.use(async (ctx, next) => {
  if (!ctx.session.sessionId) {
    ctx.session.sessionId = uuid.v4();
  }
  await next();
});

however if I do this I get (despite session id saving no problem)

{ Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:504:11)
    at ServerResponse.setHeader (_http_outgoing.js:511:3)
    at Cookies.set (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/cookies/index.js:115:13)
    at ContextSession.save (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/lib/context.js:276:22)
    at ContextSession.commit (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/lib/context.js:220:16)
    at session (/Users/yuri/Sites/bridebook/Bridebook-next/node_modules/koa-session/index.js:46:18)
    at <anonymous> headerSent: true }

Any idea what I am doing wrong?

@davegomez
Copy link

I'm getting the exact same error when trying to use koa-session + Next.js with async/await functions.

This is the code I have to write a UUID in the session:

    router.get('/', async ctx => {
      ctx.session.uuid = ctx.session.uuid || uuidv4()
      const params = { uuid: ctx.session.uuid }
      await app.render(ctx.req, ctx.res, '/', params)
      ctx.respond = false
    })

I get the error even if I use it just as a middleware (without routing) unless I write the function as not async.

@Yuripetusko
Copy link
Author

@davegomez yes we are on nextJS also

@dead-horse
Copy link
Member

koa will send body when all middleware executed. but next.js is not follow this rule for now, it will send body before middleware executed, so when middleware's downstream try to set a header, it will throw this error.

@davegomez
Copy link

Any idea how to workaround this @dead-horse?

@timneutkens from Next.js is already looking at the issue.

@dead-horse
Copy link
Member

there are lot's of middlewares will not work with next.js if it still use ctx.respond = false, for now, you can't set session in the same controller with next.js.

@dead-horse
Copy link
Member

already fixed in new koa version, it will ignore set header if body already sent.

@coler-j
Copy link

coler-j commented Nov 1, 2020

@dead-horse which version? I am on 2.13.0 (the current version) and this is still happening.

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

4 participants