Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
121
rated 0 times [  125] [ 4]  / answers: 1 / hits: 26382  / 5 Years ago, mon, august 19, 2019, 12:00:00

Typically it seems that when using Express, in the res object there is cookie so you can do something like:



res.cookie('session', sessionCookie, options);


In the API routes offered by NextJS in Next 9, when looking at res, this does not exist. Is there a way to set the cookie for a response object in a Next API Route function?


More From » node.js

 Answers
11

Adapted from offical repo middleware example, you can set Set-Cookie header via res like so:


import { serialize } from 'cookie';

function (req, res) {
// ...
// setHeader(headerName: string, cookies: string | string[])
// can use array for multiple cookies
res.setHeader('Set-Cookie', serialize('token', 'token_cookie_value', { path: '/' }));
}

[#51753] Sunday, August 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;