28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { createServerFn, createServerOnlyFn } from '@tanstack/react-start'
|
|
import { getRequest } from '@tanstack/react-start/server'
|
|
|
|
// `staff.index.tsx` and friends import `getCurrentStaffUser` from this module for
|
|
// their `beforeLoad` checks, which pulls the whole file into the client bundle too.
|
|
// `createServerOnlyFn` tells the compiler this closure (and the server-only
|
|
// `getRequest` import it uses) can be stripped from that client bundle instead of
|
|
// dragging the Postgres driver into the browser.
|
|
export const getStaffSession = createServerOnlyFn(async function getStaffSession() {
|
|
const { getAuth } = await import('#/lib/auth-instance')
|
|
return getAuth().api.getSession({ headers: getRequest().headers })
|
|
})
|
|
|
|
export async function requireStaffSession() {
|
|
const session = await getStaffSession()
|
|
|
|
if (!session) {
|
|
throw new Error('Not signed in')
|
|
}
|
|
|
|
return session
|
|
}
|
|
|
|
export const getCurrentStaffUser = createServerFn({ method: 'GET' }).handler(async () => {
|
|
const session = await getStaffSession()
|
|
return session?.user ?? null
|
|
})
|