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

session.ts

29 lines742 Bytes

1import { getIronSession } from 'iron-session'
2import { getCookie, setCookie } from '@tanstack/solid-start/server'
3import type { User } from '@prisma/client'
4​
5type SessionUser = {
6 userEmail: User['email']
7}
8​
9export function getAppSession() {
10 const password = process.env.SESSION_PASSWORD
11 if (!password || password.length < 32) {
12 throw new Error('Set SESSION_PASSWORD to at least 32 characters')
13 }
14​
15 return getIronSession<SessionUser>(
16 { read: getCookie, write: setCookie },
17 {
18 cookieName: 'app-session',
19 password,
20 ttl: 7 * 24 * 60 * 60,
21 cookieOptions: {
22 httpOnly: true,
23 sameSite: 'lax',
24 secure: process.env.NODE_ENV === 'production',
25 path: '/',
26 },
27 },
28 )