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

Is it possible to set a cookie on a serverless function in Next? #249

Closed
mirshko opened this issue Apr 3, 2020 · 8 comments
Closed

Is it possible to set a cookie on a serverless function in Next? #249

mirshko opened this issue Apr 3, 2020 · 8 comments
Labels
kind/question Developer asked a question. No code changes required.

Comments

@mirshko
Copy link

mirshko commented Apr 3, 2020

for some reason this isn't creating the cookie?

Screen Shot 2020-04-03 at 17 30 24

@maticzav
Copy link
Owner

maticzav commented Apr 4, 2020

Could you show the entire function?

@mirshko
Copy link
Author

mirshko commented Apr 4, 2020

export default async (req, res) => {
  try {
    const { email, password } = JSON.parse(req.body);

    const session = await login(email, password);

    res.status(200).json(session);
  } catch (err) {
    console.error(err);

    res.status(401).json({
      error: err.message,
    });
  }
};

right now im returning the secret returned from login and then setting it as a cookie on the client.

would probably be safer to not even pass it to the frontend at all, only set / destroy the cookies on the server side

it seems its possible to do

const cookie = serialize(TOKEN_NAME, '', {
    maxAge: -1,
    path: '/',
  })

  res.setHeader('Set-Cookie', cookie)

but this would be a diff workaround.

@mirshko
Copy link
Author

mirshko commented Apr 8, 2020

@maticzav any help here?

@stale stale bot added the stale label May 23, 2020
Repository owner deleted a comment from stale bot May 25, 2020
@stale stale bot removed the stale label May 25, 2020
@maticzav maticzav added the kind/question Developer asked a question. No code changes required. label May 25, 2020
@maticzav
Copy link
Owner

@mirshko I am sorry, I am not sure I know the answer. Perhaps someone from community could help if they spot the issue.

@leosuncin
Copy link

res needs to be wrapped with curly braces

setCookie({ res }, 'token', token, {
  httpOnly: true,
  path: '/',
  sameSite: 'strict',
});

@mirshko
Copy link
Author

mirshko commented Jun 18, 2020

oh shit! that makes sense as it’s not the whole response context object!

let me test this! thanks!

@mirshko mirshko closed this as completed Jun 18, 2020
@andrewmartin
Copy link

Hi there, I'm having the exact same issue here. I'm using more or less the exact same sample code. Wrapping in the { req } didn't seem to work.

It's odd because it actually does save it (when I log out the res.cookies I see it). But it doesn't persist in the browser.

const handler = (req: NextApiRequest, res: NextApiResponse) => {
  const { token } = req.body

  nookies.set({ res }, 'fromServer', 'value', {
    maxAge: 30 * 24 * 60 * 60,
    path: '/page',
  })

  // Return the `set-cookie` header so we can display it in the browser and show that it works!
  res.send({
    token: res.getHeader('Set-Cookie'), // strangely, I'm seeing it in the output here!
  })
}

Any thoughts? I'm going to try the suggestion here in the meantime. #249 (comment)

@andrewmartin
Copy link

My apologies, I fat-fingered the paste:

path: '/page'

Looks like this is working great. Thanks much and sorry to bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Developer asked a question. No code changes required.
Projects
None yet
Development

No branches or pull requests

4 participants