Open History to see the last commit
1import { getIronSession } from 'iron-session'
2import { getCookie, setCookie } from '@tanstack/react-start/server'
3β
4// Fixed secrets and insecure cookies are only for this local HTTP test fixture.
5export const sessionPassword = 'start-external-session-test-only-'.repeat(2)
6export type SessionData = {
7 user?: string
8 token?: string
9 serverFn?: string
10 middleware?: string
11 notice?: string
12 ssrCount?: number
13}
14β
15export async function updateSessionData(data: SessionData, name?: string) {
16 const session = await readSession(name)
17 Object.assign(session, data)
18 await session.save()
19 return { data: { ...session } }
20}
21β
22export async function destroySession() {
23 const session = await readSession()
24 session.destroy()
25}
26β
27export function readSession(cookieName = 'app-session') {
28 return getIronSession<SessionData>(
29 { read: getCookie, write: setCookie },
30 {
31 cookieName,
32 password: sessionPassword,
33 ttl: 3600,
34 chunk: true,
35 cookieOptions: {
36 secure: false,
37 httpOnly: true,
38 sameSite: 'lax',
39 path: '/',
40 },
41 },
42 )
43}
44β
45export async function readJson(request: Request): Promise<SessionData> {
46 return request.json()
47}