js-cookie wrong expiration date

55 Views Asked by At

I have this RefreshToken class in a React application (with TypeScript):

import Cookies from 'js-cookie';

const generateExpirationDate = () => {
    const date = new Date();
    date.setDate(date.getDate() + 30);
    return date;
};

/** RefreshToken class (Located in cookies) */
class RefreshToken {
    /** Set `refreshToken` (cookies) */
    public static set(refreshToken: string) {
        Cookies.set('refresh_token', refreshToken, { expires: generateExpirationDate() }); // 30 days
    }

    /** Get `refreshToken` (cookies) */
    public static get(): string | null {
        return Cookies.get('refresh_token') || null;
    }

    /** Remove `refreshToken` (cookies) */
    public static remove() {
        Cookies.remove('refresh_token');
    }
}

export default RefreshToken;

I am calling the set() method on November 5th, but in cookies this is the expiration field of the cookie: 2023-11-12. How is November 12th 30 days after November 5th? (The function to generate the expiration date adds 30 days to new Date()).

0

There are 0 best solutions below