HTTPOnly cookie not available in getServerSideProps on Next JS

Viewed 1484

The following HTTPOnly cookie did is not available in getServerSideProps using Next JS:

enter image description here

The jid cookie is set by an Expressjs API located at the url https://api-dev.example.com I am tried to get the cookie from an Next JS app located at the url https://localhost:3000.

In other words, the API is online but trying to get the cookie on localhost.

The getServerSideProps method used:

export const getServerSideProps = async (ctx) => {
    console.log(ctx.req.headers.cookie);

    return {
        props: {},
    };
};

Here is what is printed in the console:

_ga=GA1.1.1479601312.1604335288; 
_gid=GA1.1.1556520191.1604335288; 
_gat_gtag_...._1=1; 
_nid=ey...Cn0drV6-ImMPY

The cookie printed all have a domain localhost and are not HTTPOnly.

Any insights?

1 Answers

Looks like cookie is set at https://api-dev.example.com and you are trying to access it on localhost. you cannot read cookie from a different domain due to security reasons.

If you cannot setup api on your localhost. One workaround would be to add a host entry on your local system. Add below entry in your hosts file

localhost http://ui-dev.example.com

This will enable you to access your localhost using http://ui-dev.example.com:3000 and the cookie set on example.com will be accessible locally.

Related