Having to pass secret to getCookieCache in Next.js proxy.ts

I have a proxy.ts file:

import { getCookieCache } from 'better-auth/cookies';
import { NextRequest, NextResponse } from 'next/server';

export async function proxy(request: NextRequest) {
  const session = await getCookieCache(request);
  if (!session) {
    return NextResponse.redirect(new URL('/login', request.url));
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/app/:path*'],
};


When I login via Google (configured already) and access /app, I get an error
 ⨯ [Error [BetterAuthError]: getCookieCache requires a secret to be provided. Either pass it as an option or set the BETTER_AUTH_SECRET environment variable]


Even though I have set the AUTH_SECRET environment url. The only solution is to pass the secret in getCookieCache but I don't understand why this issue is occurring?
Solution
Resolution

So I mentioned I was using getCookieCache in proxy.ts

I resolved this issue by instead using getSessionCookie to check the session like so. Note: in the docs this way only checks for the existence of a cookie and you need to add proper session auth checks (such as using auth.api.getSession for Server Components) in your protected pages/routes (I put it in my app layout.tsx)
Was this page helpful?