express - 如何使用下一个 js 和 firebase 在 getServerSideProps 中检索会话 cookie
问题描述
我在 nextjs 项目中使用 firebase 进行身份验证。我正在使用 firebase admin SDK 在运行在不同端口上的 localhost 上的快速后端 API 上生成会话 cookie。
const cookie = await this.createSessionCookie(req.body.token)
if(cookie) {
// set cookie expiration
const expiresIn = 60 * 60 * 24 * 5 * 1000;
// Set cookie policy for session cookie.
const cookieOptions = { maxAge: expiresIn, httpOnly: true, secure: true, path: '/'};
res.cookie('session', cookie, cookieOptions);
res.end(JSON.stringify(
{
"message": "User created successfully",
"status": "success",
"data": { result }
}
));
}
但是我无法在 nextJS 的 getServerSideProps 中检索 cookie。
export async function getServerSideProps(context) {
const cookies = nookies.get(context)
if (!cookies.session) {
context.res.writeHead(302, { Location: '/auth/signin' })
context.res.end()
}
return {
props: {}
}
}
我知道这是同源策略的结果,因为下一个应用程序和后端 API 位于 localhost 的不同端口上。有没有办法可以在下一个应用程序中检索 cookie?
解决方案
原来我需要将 cookie 选项设置为 false,因为我在本地主机上。在此之后,我能够设置 cookie。
推荐阅读
- php - 将复选框添加到 WooCommerce 产品高级选项选项卡
- python - Plotly 无法在 Python 中的 Latex 标签中显示文本
- json - 试图在我的 laravel 项目中获取非对象的属性“对象”
- php - 有没有办法独立于 Limesurvey 订阅来提高每月回复?
- javascript - SVG进度圈图像旋转方向
- php - Laravel 密码确认返回 ValidationException
- oracle - 表的循环列的Oracle结构
- java - Thymeleaf + Spring boot - 使用getter方法显示图片
- python - Python 上一行值
- c# - 为什么 Html.Action 在找不到控制器时失败?