I am trying to parse a cookie which was set by including the property httpOnly: true
on my custom express server
Now in my NextJs app I can fetch the cookie in the server side method getServerSideProps
and access the cookie in ctx.req.cookies
from there. But when I make a fetch api call (from the server side method) to the custom server, the cookie does not seem to be accessible in the custom server api call.
Here is my express
configuration in server.ts
:
// Express Configuration
app.set('port', process.env.PORT || 3000)
app.use(cors({ credentials: true, origin: true }))
app.use(cookieParser())
app.use(passport.initialize())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
This is how I set the cookie on custom express server:
res.cookie('rtk', refreshTokenJWT.token, {
httpOnly: true,
})
This is the fetch api call from getServerSideProps
method:
const response = await fetch('http://localhost:3000/refresh_token', {
credentials: 'include',
method: 'POST',
})
Perhaps it is because of the fact that I have to make the api call with absolute url, when I try to do fetch('/refresh_token', ...)
I get the error: TypeError: Only absolute URLs are supported
I could possibly send the payload from the server side method in the request body
and handle it in the custom express server, but this does not seem like the best solution.
pls elp, thanks in advance.
Edit: To clarify, the await fetch()
is happening in the server side method getServerSideProps
of nextjs.
Sending the cookie as a header in the fetch
api call as suggested by @O. Jones works, but I was under the impression that this wouldn't work since I had set httpOnly: true
.