Next.js Discord

Discord Forum

cookies-next clears cookie and returned undefined after 5-mins

Unanswered
Pavement ant posted this in #help-forum
Open in Discord
Pavement antOP
I have this weird behaviour using cookies-next to store the jwt returned from the login endpoint, after 5 mins of making Endpoint calls the application logged out. This is because I implemented my axios client to route back to /login upon 401 status code.

Below is my code to store and get cookie

import { deleteCookie, getCookie, setCookie } from 'cookies-next';

interface StorageProps {
key: string;
value?: string | unknown;
}

export const storeCookie = ({ key, value }: StorageProps): void => {
const date = new Date();
const expireTime = new Date(date.getTime() + 7 * 60 * 60 * 1000);
const maxAge = 7 * 60 * 60;

if (key !== '' && value !== '') {
setCookie(key, value, {
expires: expireTime,
maxAge,
domain: '',
path: '/',
sameSite: 'strict',
});
}
};

export const getAuthToken = (): string | undefined => {
const authToken = getCookie(AUTH_TOKEN, {
domain: '',
path: '/',
sameSite: 'strict',
});

return typeof authToken === 'string' ? authToken : undefined;
};

0 Replies