I have this function in my next.js app :
const getTokenFromCookie = (req) => {
const parseCookies = (cookieString) => {
if (!cookieString) {
return {};
}
return Object.fromEntries(
cookieString.split("; ").map((cookie) => {
const [name, ...rest] = cookie.split("=");
return [name, rest.join("=")];
})
);
};
const cookies = parseCookies(req.headers.cookie);
const token = cookies._token || null;
return token;
};
I’m using it inside getServersideProps like this :
export async function getServerSideProps({ req, locale }) {
const core = process.env.NEXT_PUBLIC_API_ENDPOINT;
const token = getTokenFromCookie(req);
const headers = token ? { Authorization: `Bearer ${token}` } : {};
/////////////////////////////
On my localhost everithing is working normal but on remote server the token is null,
How to fix this issue ?
I expect to get token from the cookies
New contributor
dragan belegic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.