Im using next.js and strapi. I am trying to set a httpOnly cookie between my next js front-end and my strapi app.
The cookie is recieved by the backend however when i try to make a request to the backend - The cookie is not present. However when i use postman the cookie set is present.
Strapi app:
const t = await ctx.cookies.get('mytest') // prints undefined with next js but works with postman
console.log(t);
ctx.cookies.set('mytest', '123', {
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 365,
secure: false,
})
next.js frontend:
export default function Home(props: any) {
async function onClick() {
const options = {
method: 'GET',
'Access-Control-Allow-Credentials': true,
withCredenitals: true
}
const test = await fetch('http://localhost:1337/endpoint', options as any)
const res = await test.json()
}
return (
<div>
<div onClick={onClick}>
Make a fetch to the api
</div>
</div>
)
}
Things i have tried:
const options = {
method: 'get',
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'include', // include, *same-origin, omit
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials': true,
"Access-Control-Allow-Origin": "http://127.0.0.1:1337/myapi"
},
}
const req = await fetch('http://127.0.0.1:1337/myapi', options)
const res = await req.json()
console.log(t);
ctx.cookies.set('mytest', '123', {
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 365,
secure: false,
})
ctx.response.header = {
...ctx.response.header,
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': ctx.request.headers.origin,
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,UPDATE,OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
}
No luck.
Can anyone tell whats going on? I have been searching for hours but nothing seems to work
I eventually fixed this - The issue was infact because the cookies were not being saved into my browser.
There were two issues:
credentials: 'include',
ctx.set('Access-Control-Allow-Origin', ctx.request.headers.origin)
Hope this helps and you dont spend 8 hours randomly console logging stuff