-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Comments
Could you show the entire function? |
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 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. |
@maticzav any help here? |
@mirshko I am sorry, I am not sure I know the answer. Perhaps someone from community could help if they spot the issue. |
setCookie({ res }, 'token', token, {
httpOnly: true,
path: '/',
sameSite: 'strict',
}); |
oh shit! that makes sense as it’s not the whole response context object! let me test this! thanks! |
Hi there, I'm having the exact same issue here. I'm using more or less the exact same sample code. Wrapping in the It's odd because it actually does save it (when I log out the 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) |
My apologies, I fat-fingered the paste:
Looks like this is working great. Thanks much and sorry to bug! |
for some reason this isn't creating the cookie?
The text was updated successfully, but these errors were encountered: