Vitron-Front/app/sessions.server.ts
2026-04-29 01:44:16 +03:30

35 lines
1.4 KiB
TypeScript

import { createCookieSessionStorage, Session } from "@remix-run/node";
import { isProduction } from "./utils/get-env";
// export the whole sessionStorage object
export const sessionStorage = createCookieSessionStorage({
cookie: {
name: "vitrown_session", // use name you want here
sameSite: "lax", // this helps with CSRF
path: "/", // remember to add this so the cookie will work in all routes
httpOnly: true, // for security reasons, make this cookie http only
secrets: [process.env.REMIX_SECRET ?? "secret"], // replace this with an actual secret
secure: isProduction(), // enable this in prod only
},
});
export const { getSession, commitSession, destroySession } = sessionStorage;
export interface UserTrackerSessionType {
token: string;
session: Session;
}
// Helper function to check if access_token is expired (1 week)
export function isAccessTokenExpired(tokenTimestamp: number): boolean {
const oneWeekInMs = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
const currentTime = Date.now();
return currentTime - tokenTimestamp > oneWeekInMs;
}
// Helper function to check if refresh_token is expired (1 month)
export function isRefreshTokenExpired(tokenTimestamp: number): boolean {
const oneMonthInMs = 30 * 24 * 60 * 60 * 1000; // ~30 days in milliseconds
const currentTime = Date.now();
return currentTime - tokenTimestamp > oneMonthInMs;
}