next.js Js 13 Fetch不返回cookie

2guxujil  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(158)

我试图用NEXT JS 13获取我的登录API端点,据说在成功登录后会发出access_token和refresh_token cookie,不幸的是浏览器中没有cookie出现,但在我的控制台日志中显示有cookie正在设置,这是一个bug还是什么?(它以前在NEXT JS 12中工作)

import React from 'react'
import TodoList from './TodoList'

const fetchSingle = async () => {
    const res = await fetch
        (`http://localhost:3000/auth/signin`, {
            credentials: 'include',
            method: 'POST',
            headers: {
                'Accept': 'application/json, text/plain, */*',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ email: 'test@gmail.com', password: '12345' })
        })
    try {
        return res
    } catch (error) {
        return console.log('Error')
    }

}

export default async function Todos() {

    const response = await fetchSingle()
    console.log(response)

    return (
        <div>
            {/* @ts-ignore */}
            <TodoList />
        </div>
    )
}
guicsvcw

guicsvcw1#

我不确定,但我认为您必须通过将“凭据”设置为“包含”来启用跨域Cookie:

fetch(url, {
    cache: 'force-cache',       // SSG  (getStaticProps)  // default
    cache: 'no-store',          // SSR  (getServerSideProps)
    next: { revalidate: 60 },  // ISR
    credentials: 'include'      // cross-origin cookies
})

相关问题