Skip to content

Commit 96730fa

Browse files
committed
Aggregate the session length values
1 parent 5a8b888 commit 96730fa

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

apps/webapp/app/services/sessionDuration.server.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import type { Session } from "@remix-run/node";
22
import type { PrismaClientOrTransaction } from "@trigger.dev/database";
33
import { 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

68
export 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
1213
const GREGORIAN_HALF_YEAR_SECONDS = 15_778_476;
1314

14-
export const DEFAULT_SESSION_DURATION_SECONDS = GREGORIAN_YEAR_SECONDS;
15-
1615
export 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

3130
export 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
*/
172168
export 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
}

apps/webapp/app/services/sessionStorage.server.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { createCookieSessionStorage } from "@remix-run/node";
22
import { env } from "~/env.server";
33

4-
// Hard ceiling for the cookie lifetime. The actual per-session value is set
5-
// per-commit via commitSession(session, { maxAge }) in the auth/login flows
6-
// and on every authenticated response, derived from the user's effective
7-
// session duration (User.sessionDuration capped by Organization.maxSessionDuration).
8-
export const SESSION_STORAGE_MAX_AGE_SECONDS = 60 * 60 * 24 * 365;
4+
// Canonical "1 year in seconds", using Gregorian calendar conversion
5+
// (365.2425 * 86400) so it matches the labeled "1 year" dropdown option in
6+
// SESSION_DURATION_OPTIONS exactly. This is the cookie's hard upper-bound
7+
// lifetime; the actual per-session value is enforced server-side via
8+
// `sessionIssuedAt` against the user's effective duration.
9+
export const DEFAULT_SESSION_DURATION_SECONDS = 31_556_952;
910

1011
export const sessionStorage = createCookieSessionStorage({
1112
cookie: {
@@ -15,7 +16,7 @@ export const sessionStorage = createCookieSessionStorage({
1516
httpOnly: true,
1617
secrets: [env.SESSION_SECRET],
1718
secure: env.NODE_ENV === "production",
18-
maxAge: SESSION_STORAGE_MAX_AGE_SECONDS,
19+
maxAge: DEFAULT_SESSION_DURATION_SECONDS,
1920
},
2021
});
2122

0 commit comments

Comments
 (0)