【发布时间】:2020-09-13 19:51:04
【问题描述】:
我正在为我的应用程序使用 Nextjs。在页面上,我想从经过身份验证的 API 端点('/api/profile')获取数据。
我尝试了以下方法,但没有成功:
export async function getServerSideProps(ctx) {
const { req, res } = ctx
const cookies = cookie.parse(req.headers.cookie ?? '')
const mycookie = cookies[MY_COOKIE] // mycookie exists and is set correctly
if (mycookie) {
const response = await fetch(process.env.SERVER_HOST+'/api/profile', {
credentials: 'same-origin' // I tried with and without this, also tried "include" instead
})
...
我有两个问题:
- 有没有办法避免输入绝对 URL? (我希望简单地使用“/api/profile”,因为它是一个“内部”api)
- 如何确保从 /api/profile 获取数据所需的 cookie 是通过
fetch转发的?
注意:我的 cookie 是 httpOnly。
【问题讨论】: