Skip to content

Commit d622731

Browse files
committed
Make the default session TTL configurable via T3CODE_SESSION_TTL
Paired sessions hardcode a 30-day expiry, so every client ages out monthly and must re-pair. SessionStore.issue now resolves its default from T3CODE_SESSION_TTL (days, positive finite number; unset or invalid keeps 30 days), read at issue time so the setting applies without a server restart.
1 parent 46140c9 commit d622731

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/server/src/auth/SessionStore.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,18 @@ it.layer(NodeServices.layer)("SessionStore.layer", (it) => {
579579
expect((yield* readRow)[0]).toEqual({ surface: "mobile", appVersion: "1.3.0" });
580580
}).pipe(Effect.provide(Layer.mergeAll(makeSessionStoreLayer(), SqlitePersistenceMemory))),
581581
);
582+
it.effect("extends the default session TTL from T3CODE_SESSION_TTL", () =>
583+
Effect.gen(function* () {
584+
const sessions = yield* SessionStore.SessionStore;
585+
const before = process.env.T3CODE_SESSION_TTL;
586+
process.env.T3CODE_SESSION_TTL = "90";
587+
try {
588+
const issued = yield* sessions.issue({ subject: "env-ttl-probe" });
589+
expect(issued.expiresAt.epochMilliseconds).toBe(Duration.toMillis(Duration.days(90)));
590+
} finally {
591+
if (before === undefined) delete process.env.T3CODE_SESSION_TTL;
592+
else process.env.T3CODE_SESSION_TTL = before;
593+
}
594+
}).pipe(Effect.provide(makeSessionStoreLayer())),
595+
);
582596
});

‎apps/server/src/auth/SessionStore.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ export class SessionStore extends Context.Service<
415415

416416
const SIGNING_SECRET_NAME = "server-signing-key";
417417
const DEFAULT_SESSION_TTL = Duration.days(30);
418+
419+
/** Default session lifetime in days; overrides the 30-day default when a
420+
* positive finite number, so long-lived or headless deployments can extend
421+
* paired sessions without re-pairing every month. */
422+
function defaultSessionTtl(): Duration.Duration {
423+
const raw = process.env.T3CODE_SESSION_TTL;
424+
if (!raw) return DEFAULT_SESSION_TTL;
425+
const days = Number(raw);
426+
return Number.isFinite(days) && days > 0 ? Duration.days(days) : DEFAULT_SESSION_TTL;
427+
}
418428
const DEFAULT_WEBSOCKET_TOKEN_TTL = Duration.minutes(5);
419429

420430
const SessionClaims = Schema.Struct({
@@ -625,7 +635,7 @@ export const make = Effect.gen(function* () {
625635
);
626636
const issuedAt = yield* DateTime.now;
627637
const expiresAt = DateTime.add(issuedAt, {
628-
milliseconds: Duration.toMillis(input?.ttl ?? DEFAULT_SESSION_TTL),
638+
milliseconds: Duration.toMillis(input?.ttl ?? defaultSessionTtl()),
629639
});
630640
const claims: SessionClaims = {
631641
v: 1,

0 commit comments

Comments
 (0)