feat: switch to config file system

This commit is contained in:
Aarnav Tale
2025-02-13 12:35:12 -05:00
parent 5be3cb345e
commit 76d263b7e6
28 changed files with 1071 additions and 883 deletions
+22 -4
View File
@@ -1,4 +1,8 @@
import { Session, SessionStorage, createCookieSessionStorage } from 'react-router';
import {
Session,
SessionStorage,
createCookieSessionStorage,
} from 'react-router';
export type SessionData = {
hsApiKey: string;
@@ -23,7 +27,7 @@ type SessionStore = SessionStorage<SessionData, SessionFlashData>;
// TODO: Add args to this function to allow custom domain/config
let sessionStorage: SessionStore | null = null;
export function initSessionManager() {
export function initSessionManager(secret: string, secure: boolean) {
if (sessionStorage) {
return;
}
@@ -35,8 +39,8 @@ export function initSessionManager() {
maxAge: 60 * 60 * 24, // 24 hours
path: '/',
sameSite: 'lax',
secrets: [process.env.COOKIE_SECRET!],
secure: process.env.COOKIE_SECURE !== 'false',
secrets: [secret],
secure,
},
});
}
@@ -49,6 +53,20 @@ export function getSession(cookie: string | null) {
return sessionStorage.getSession(cookie);
}
export async function auth(request: Request) {
if (!sessionStorage) {
return false;
}
const cookie = request.headers.get('Cookie');
const session = await sessionStorage.getSession(cookie);
if (!session.has('hsApiKey')) {
return false;
}
return true;
}
export function destroySession(session: Session) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');