11import type { Session } from "@remix-run/node" ;
22import type { PrismaClientOrTransaction } from "@trigger.dev/database" ;
33import { prisma } from "~/db.server" ;
4- import { commitSession } from "./sessionStorage.server" ;
4+ import { commitSession , DEFAULT_SESSION_DURATION_SECONDS } from "./sessionStorage.server" ;
5+
6+ export { DEFAULT_SESSION_DURATION_SECONDS } ;
57
68export const SESSION_ISSUED_AT_KEY = "session:issuedAt" ;
79
810// Months and years use standard Gregorian-calendar conversions (365.2425 days/yr,
911// 30.436875 days/month) so values produced by external "X months in seconds"
1012// calculators map cleanly to a labeled option.
11- const GREGORIAN_YEAR_SECONDS = 31_556_952 ; // 365.2425 * 86400
1213const GREGORIAN_HALF_YEAR_SECONDS = 15_778_476 ;
1314
14- export const DEFAULT_SESSION_DURATION_SECONDS = GREGORIAN_YEAR_SECONDS ;
15-
1615export type SessionDurationOption = {
1716 value : number ;
1817 label : string ;
@@ -25,7 +24,7 @@ export const SESSION_DURATION_OPTIONS: SessionDurationOption[] = [
2524 { value : 60 * 60 * 24 , label : "1 day" } ,
2625 { value : 60 * 60 * 24 * 30 , label : "30 days" } ,
2726 { value : GREGORIAN_HALF_YEAR_SECONDS , label : "6 months" } ,
28- { value : GREGORIAN_YEAR_SECONDS , label : "1 year" } ,
27+ { value : DEFAULT_SESSION_DURATION_SECONDS , label : "1 year" } ,
2928] ;
3029
3130export const ALLOWED_SESSION_DURATION_VALUES : ReadonlySet < number > = new Set (
@@ -155,27 +154,24 @@ export function ensureSessionIssuedAt(session: Session, now: number = Date.now()
155154 return true ;
156155}
157156
158- /**
159- * The auth cookie's `Max-Age` is intentionally long (1 year) so the cookie
160- * always reaches the server. Actual session expiry is enforced server-side
161- * via `sessionIssuedAt` against the user's effective duration. If we let the
162- * cookie expire client-side, the user is silently logged out without the
163- * "signed out due to inactivity" toast.
164- */
165- const AUTH_COOKIE_MAX_AGE_SECONDS = DEFAULT_SESSION_DURATION_SECONDS ;
166-
167157/**
168158 * Commits the session for an authenticated user, setting `issuedAt = now`.
169159 * Use this at every login/MFA-completion point so the session window starts
170- * fresh. Cookie `Max-Age` is fixed; expiry is enforced server-side.
160+ * fresh.
161+ *
162+ * The auth cookie's `Max-Age` is intentionally long
163+ * (`DEFAULT_SESSION_DURATION_SECONDS`, 1 year) so the cookie always reaches
164+ * the server. Actual session expiry is enforced server-side via
165+ * `sessionIssuedAt` against the user's effective duration. If we let the
166+ * cookie expire client-side, the user is silently logged out.
171167 */
172168export async function commitAuthenticatedSession (
173169 session : Session ,
174170 _userId : string ,
175171 now : number = Date . now ( )
176172) : Promise < string > {
177173 setSessionIssuedAt ( session , now ) ;
178- return commitSession ( session , { maxAge : AUTH_COOKIE_MAX_AGE_SECONDS } ) ;
174+ return commitSession ( session , { maxAge : DEFAULT_SESSION_DURATION_SECONDS } ) ;
179175}
180176
181177/**
@@ -189,5 +185,5 @@ export async function commitAuthenticatedSessionLazy(
189185 now : number = Date . now ( )
190186) : Promise < string > {
191187 ensureSessionIssuedAt ( session , now ) ;
192- return commitSession ( session , { maxAge : AUTH_COOKIE_MAX_AGE_SECONDS } ) ;
188+ return commitSession ( session , { maxAge : DEFAULT_SESSION_DURATION_SECONDS } ) ;
193189}
0 commit comments