fix: rely on auth API for server session lookup
This commit is contained in:
parent
bad090ab0d
commit
83aabce8cc
1 changed files with 6 additions and 4 deletions
|
|
@ -1,12 +1,11 @@
|
||||||
import { cookies, headers } from "next/headers"
|
import { cookies, headers } from "next/headers"
|
||||||
import { redirect } from "next/navigation"
|
import { redirect } from "next/navigation"
|
||||||
|
|
||||||
import { getCookieCache } from "better-auth/cookies"
|
|
||||||
|
|
||||||
import { env } from "@/lib/env"
|
import { env } from "@/lib/env"
|
||||||
import { isAdmin, isStaff } from "@/lib/authz"
|
import { isAdmin, isStaff } from "@/lib/authz"
|
||||||
|
import { auth } from "@/lib/auth"
|
||||||
|
|
||||||
type ServerSession = Awaited<ReturnType<typeof getCookieCache>>
|
type ServerSession = Awaited<ReturnType<typeof auth.api.getSession>>
|
||||||
|
|
||||||
async function serializeCookies() {
|
async function serializeCookies() {
|
||||||
const store = await cookies()
|
const store = await cookies()
|
||||||
|
|
@ -43,7 +42,10 @@ async function buildRequest() {
|
||||||
export async function getServerSession(): Promise<ServerSession | null> {
|
export async function getServerSession(): Promise<ServerSession | null> {
|
||||||
try {
|
try {
|
||||||
const request = await buildRequest()
|
const request = await buildRequest()
|
||||||
const session = await getCookieCache(request)
|
const session = await auth.api.getSession({
|
||||||
|
headers: request.headers,
|
||||||
|
request,
|
||||||
|
})
|
||||||
return session ?? null
|
return session ?? null
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to read Better Auth session", error)
|
console.error("Failed to read Better Auth session", error)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue