forked from LibreChat-AI/admin-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.ts
More file actions
57 lines (46 loc) · 1.81 KB
/
Copy pathsession.ts
File metadata and controls
57 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useSession } from '@tanstack/react-start/server';
import type * as t from '@/types';
const DEV_SECRET = 'dev-only-session-secret-minimum-32-chars!';
const MIN_SESSION_SECRET_LENGTH = 32;
const REVALIDATION_INTERVAL_MS = 60_000;
const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
const envIdleTimeout = Number(process.env.ADMIN_SESSION_IDLE_TIMEOUT_MS);
const effectiveIdleTimeout =
Number.isFinite(envIdleTimeout) && envIdleTimeout > 0 ? envIdleTimeout : DEFAULT_IDLE_TIMEOUT_MS;
const sessionCookieSecure =
process.env.SESSION_COOKIE_SECURE !== undefined
? process.env.SESSION_COOKIE_SECURE === 'true'
: process.env.NODE_ENV === 'production';
export const SESSION_CONFIG = {
revalidationInterval: REVALIDATION_INTERVAL_MS,
idleTimeout: effectiveIdleTimeout,
} as const;
const sessionSecret =
process.env.SESSION_SECRET || (process.env.NODE_ENV === 'development' ? DEV_SECRET : undefined);
if (!sessionSecret) {
throw new Error('SESSION_SECRET environment variable must be set for admin session encryption.');
}
if (sessionSecret.length < MIN_SESSION_SECRET_LENGTH) {
throw new Error(
`SESSION_SECRET must be at least ${MIN_SESSION_SECRET_LENGTH} characters for admin session encryption.`,
);
}
if (!process.env.SESSION_SECRET && process.env.NODE_ENV === 'development') {
console.warn(
'[session] Using hardcoded DEV_SECRET — set SESSION_SECRET for production-like environments',
);
}
const sessionCookiePath = process.env.VITE_BASE_PATH || '/';
export function useAppSession(): ReturnType<typeof useSession<t.SessionData>> {
return useSession<t.SessionData>({
name: 'admin-session',
password: sessionSecret || '',
cookie: {
path: sessionCookiePath,
secure: sessionCookieSecure,
sameSite: 'lax',
httpOnly: true,
maxAge: 60 * 60 * 24 * 7,
},
});
}