Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

session.ts

47 lines1.2 KB

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
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 } }
21​
22export async function destroySession() {
23 const session = await readSession()
24 session.destroy()
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 )
44​
45export async function readJson(request: Request): Promise<SessionData> {
46 return request.json()