fix: normalize server session expiresAt for better-auth

This commit is contained in:
Esdras Renan 2025-10-18 22:30:06 -03:00
parent 83aabce8cc
commit 0e27d6b113

View file

@ -5,7 +5,21 @@ import { env } from "@/lib/env"
import { isAdmin, isStaff } from "@/lib/authz" import { isAdmin, isStaff } from "@/lib/authz"
import { auth } from "@/lib/auth" import { auth } from "@/lib/auth"
type ServerSession = Awaited<ReturnType<typeof auth.api.getSession>> type ServerSession = {
session: {
id: string
expiresAt: number
}
user: {
id: string
name: string
email: string
role: string
tenantId: string | null
avatarUrl: string | null
machinePersona: string | null
}
}
async function serializeCookies() { async function serializeCookies() {
const store = await cookies() const store = await cookies()
@ -46,7 +60,33 @@ export async function getServerSession(): Promise<ServerSession | null> {
headers: request.headers, headers: request.headers,
request, request,
}) })
return session ?? null if (!session) return null
const expiresValue = session.session.expiresAt
const expiresAt =
expiresValue instanceof Date
? expiresValue.getTime()
: typeof expiresValue === "number"
? expiresValue
: expiresValue
? new Date(expiresValue).getTime()
: Date.now()
return {
session: {
id: session.session.id,
expiresAt,
},
user: {
id: session.user.id,
name: session.user.name,
email: session.user.email,
role: (session.user as { role?: string }).role ?? "agent",
tenantId: (session.user as { tenantId?: string | null }).tenantId ?? null,
avatarUrl: (session.user as { avatarUrl?: string | null }).avatarUrl ?? null,
machinePersona: (session.user as { machinePersona?: string | null }).machinePersona ?? null,
},
}
} catch (error) { } catch (error) {
console.error("Failed to read Better Auth session", error) console.error("Failed to read Better Auth session", error)
return null return null