Can't set a cookie using NextJS 9's API Route

杀马特。学长 韩版系。学妹 提交于 2020-05-29 08:07:46

问题


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?


回答1:


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

import { serialize } from 'cookie';

function (req, res) {
   // ...
   res.setHeader('Set-Cookie', serialize('token', 'token_cookie_value', { path: '/' }));
}


来源:https://stackoverflow.com/questions/57559239/cant-set-a-cookie-using-nextjs-9s-api-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!