Within my Next.js application, I am serving cookies from the server-side API like this:
res.setHeader('Set-Cookie', AJWT)
res.redirect(302, '/')
Now, in my index.js
file, I am attempting to retrieve the cookie before the page is rendered:
export async function getServerSideProps(context) {
let cookies = context.req.headers.cookie
if (typeof cookies !== 'string') {
return {
props: { auth: false },
}
} else {
const { AJWT } = cookie.parse(cookies)
return {
props: { auth: AJWT ? true : false },
}
}
}
After the initial redirect, the first render does not have access to the cookies. However, upon refreshing, they are successfully captured. Is there a way to ensure that the cookies are available on the very first render?
UPDATE: It should be noted that the cookie is indeed visible in my devtools when I am initially redirected.