1

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?

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.