I am creating a react application with using cookies.Here is my class.
export class Auth {
static config = {
domain: `${process.env.GRP_APP_HOST?.includes('localhost') ? '.' + process.env.GRP_APP_HOST : ''}`,
};
static setToken(token: string) {
const now = new Date();
const minutes = 30;
now.setTime(now.getTime() + (minutes * 60 * 1000));
if (token) {
document.cookie = `auth_token=${token}; domain=${this.config.domain}; expires=${now.toUTCString()}`;
}
}
static getToken() {
return document.cookie
.split('; ')
.find(row => row.startsWith('auth_token='))
?.split('=')[1];
}
static clear() {
document.cookie = 'auth_token=; Max-Age=0; expires = Thu, 01 Jan 1970 00:00:00 GMT;';
}
}
I am setting the cookie to expire after 30 minutes. The problem is that currently, I am in GMT +4 timezone and the cookie expiration date is being calculated wrong ( -3.5 hours ).I know that cookies expiration date format should be UTC or GMT format. How should I set the correct format that will also cover the timezone differences ?