I created authentications using Better-Auth and Google as the auth client. I have the correct URL for the authorized JavaScript origins and for the authorized redirect URL and I believe my set up is correct for the middleware authentication. I continue to get an Internal Server Error for POST on my Vercel deployment, and when running this in development I get this (from the terminal):
ERROR [Better Auth]: INTERNAL_SERVER_ERROR TypeError: Cannot read properties of undefined (reading 'map')
at async middleware (middleware.ts:8:20)
6 |
7 | export async function middleware(request: NextRequest, response: NextResponse) {
> 8 | const session = await auth.api.getSession({
| ^
9 | headers: await headers()
10 | })
11 | if(!session) { {
}
⨯ [Error [APIError]: Failed to get session]
This is the full function:
export async function middleware(request: NextRequest, response: NextResponse) {
const session = await auth.api.getSession({
headers: await headers()
})
if(!session) {
return NextResponse.redirect(new URL('/sign-in', request.url))
}
return NextResponse.next();
}
I'm confused as to what's wrong with this function implementation. What do I need to change to correct this error?
catchblock on line 245 ofsession.ts. I'd suggest using an interactive debugger to narrow it down further and determine which value is actuallyundefined.