2024-11-06 17:49:18 +00:00
|
|
|
import { config } from '$lib/server/api/common/config';
|
|
|
|
|
import env from '$lib/server/api/common/env';
|
2024-11-08 01:34:13 +00:00
|
|
|
import { TimeSpan } from 'oslo';
|
|
|
|
|
|
|
|
|
|
export const cookieMaxAge = 60 * 60 * 24 * 30;
|
|
|
|
|
export const cookieExpiresMilliseconds = new TimeSpan(2, 'w').milliseconds();
|
|
|
|
|
export const cookieExpiresAt = new Date(Date.now() + cookieExpiresMilliseconds);
|
|
|
|
|
export const halfCookieExpiresMilliseconds = cookieExpiresMilliseconds / 2;
|
|
|
|
|
export const halfCookieExpiresAt = new Date(Date.now() + halfCookieExpiresMilliseconds);
|
|
|
|
|
export const cookieName = 'session';
|
2024-11-06 17:49:18 +00:00
|
|
|
|
|
|
|
|
export function createSessionTokenCookie(token: string, expiresAt: Date) {
|
|
|
|
|
return {
|
2024-11-08 01:34:13 +00:00
|
|
|
name: cookieName,
|
2024-11-06 17:49:18 +00:00
|
|
|
value: token,
|
|
|
|
|
attributes: {
|
|
|
|
|
path: '/',
|
2024-11-08 01:34:13 +00:00
|
|
|
maxAge: cookieMaxAge,
|
2024-11-06 17:49:18 +00:00
|
|
|
domain: env.DOMAIN,
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
secure: config.isProduction,
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
expires: expiresAt,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createBlankSessionTokenCookie() {
|
|
|
|
|
return {
|
2024-11-08 01:34:13 +00:00
|
|
|
name: cookieName,
|
2024-11-06 17:49:18 +00:00
|
|
|
value: '',
|
|
|
|
|
attributes: {
|
|
|
|
|
path: '/',
|
|
|
|
|
maxAge: 0,
|
|
|
|
|
domain: env.DOMAIN,
|
|
|
|
|
sameSite: 'lax',
|
|
|
|
|
secure: config.isProduction,
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
expires: new Date(0),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|