[▲ Vercel Community](/) · [Categories](/categories) · [Latest](/latest) · [Top](/top) · [Live](/live) [Help](/c/help/9) # Next auth api route throwing Error: Failed to collect page data for /api/auth/[...nextauth] only on production 1553 views · 6 likes · 22 posts Kaungmyatkyaw2 (@kaungmyatkyaw2) · 2024-07-17 ### Summary I am using next auth for authentication. It is okay on local build. But when it is on vercel deploy production build it is throwing following error. ` at 74088 (/vercel/path0/.next/server/app/api/auth/[...nextauth]/route.js:1:1513) at t (/vercel/path0/.next/server/webpack-runtime.js:1:128) at r (/vercel/path0/.next/server/app/api/auth/[...nextauth]/route.js:39:88504) at /vercel/path0/.next/server/app/api/auth/[...nextauth]/route.js:39:88531 at t.X (/vercel/path0/.next/server/webpack-runtime.js:1:1196) at /vercel/path0/.next/server/app/api/auth/[...nextauth]/route.js:39:88517 at Object.<anonymous> (/vercel/path0/.next/server/app/api/auth/[...nextauth]/route.js:39:88559) at Module._compile (node:internal/modules/cjs/loader:1358:14) { clientVersion: '5.17.0', errorCode: undefined } Build error occurred Error: Failed to collect page data for /api/auth/[...nextauth] at /vercel/path0/node_modules/next/dist/build/utils.js:1268:15 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { type: 'Error' } Error: Command "npm run build" exited with 1 ` #### This is my api/auth/[...nextauth]/route.ts import { authOptions } from '@/lib/auth'; import NextAuth from 'next-auth/next'; const handler = NextAuth(authOptions); export { handler as GET, handler as POST }; #### This is my lib/auth.ts import { AuthOptions, getServerSession } from 'next-auth'; import CredentialsProvider from 'next-auth/providers/credentials'; import { PrismaAdapter } from '@next-auth/prisma-adapter'; import { prisma } from '@/lib/prisma'; import bcrypt from 'bcrypt'; export const authOptions: AuthOptions = { adapter: PrismaAdapter(prisma), session: { strategy: 'jwt', }, secret: process.env.NEXTAUTH_SECRET, providers: [ CredentialsProvider({ name: 'credentials', credentials: { email: { label: 'email:', type: 'text', placeholder: 'your-email', }, password: { label: 'password:', type: 'password', placeholder: 'your-password', }, }, async authorize(credentials) { const user = await prisma.user.findUnique({ where: { email: credentials?.email }, }); if (!user) { throw new Error("User doesn't exist"); } const isPasswordMatch = await bcrypt.compare( credentials?.password!, user?.password!, ); if (!isPasswordMatch) { throw new Error('Invalid Password!'); } return user; }, }), ], callbacks: { async signIn() { return true; }, async jwt({ token }) { const dbUser = await prisma.user.findUnique({ where: { email: token.email! } }); if (!dbUser) { throw new Error('No user is found with this email!'); } return { id: dbUser.id, name: dbUser.name, email: dbUser.email, picture: dbUser.image, }; }, async session({ session, token }) { if (token) { session.user = { name: token.name, email: token.email, image: token.picture, }; } return session; }, }, } satisfies AuthOptions; export function getSession() { return getServerSession(authOptions); } this is my .env and env variable that i put on production build DATABASE_URL=DBURL NEXTAUTH_SECRET=3ijfdl2393djDf919 NEXTAUTH_URL=http://localhost:3000 I have tried several solution but no one solve my situation ### Example _No response_ ### Steps to Reproduce Just deploy my app on vercel Pauline P. Narvas (@pawlean) · 2024-07-18 Hi @kaungmyatkyaw2! Could it be your environment variables? From what you shared: > DATABASE_URL=**DBURL** NEXTAUTH_SECRET=3ijfdl2393djDf919 NEXTAUTH_URL=**[http://localhost:3000](http://localhost:3000/)** You don't have a `DATABASE_URL` set and your `NEXTAUTH_URL` points to your localhost. Kaungmyatkyaw2 (@kaungmyatkyaw2) · 2024-07-18 Thanks for ur reply.But I don't know what u wanna mean. By my understanding, Do u mean I have to set real database url? If it is , I have set real database url The shown one is just dummy purpose. Gtmetric (@gtmetric) · 2024-07-20 · ♥ 1 I'm having the same issue. 3den (@3den) · 2024-07-22 Auth stoped working for me on friday, even deploying previous commit does not work. Amy Egan (@amyegan) · 2024-07-22 It could be the result of an expired key. Can you verify that the existing environment variables are still valid? 3den (@3den) · 2024-07-22 Its the same ENV i use in localhost and also the same one used on the deploy I reverted too. I added some logs to the server and middleware the supabase login does work and fetches the user correctly, so it sends to the correct page after login, but since the cookie is lost navigating to any page will result in a logout. 3den (@3den) · 2024-07-22 On the version that works i get this cookies (Reverted Prop):  On the one that breaks I get this (Any previews or recent deploy):  Notice that sb-api-auth-token is missing. I get "vercel-experiment-uuid" this is new to me, what is it suppose todo? 3den (@3den) · 2024-07-23 seems like vercel has some middleware that sets cookies, anyway i can disable that? that behaiviour does not happen in localhost Planify La (@planify-la) · 2024-07-23 · ♥ 1 We are experiencing this problem and thus affecting end users. Planify La (@planify-la) · 2024-07-23 · ♥ 1 I opened a ticket because we have Pro plan. We already reverted a branch to a two months old version that worked perfectly, but now vercel throw errors in all server actions and the middleware is not storing the cookies. 3den (@3den) · 2024-07-23 Thanks, please let me know if you hear something from then. Currently im looking at other options to migrate away from vercel. Planify La (@planify-la) · 2024-07-23 I deployed to Render and Amplify and the issue is still happening. I think it is related to nextjs Amy Egan (@amyegan) · 2024-07-23 The output saying `Build error occurred Error: Failed to collect page data for /api/auth/[...nextauth]` means *something* went wrong when attempting to build that file. We need more information to figure out what it was. Just to confirm we're not missing something with a simple solution... I know you mentioned that you only used `DBURL` as a placeholder to avoid revealing the actual `DATABASE_URL`, but did you also update `NEXTAUTH_URL` to the actual deployment URL instead of `http://localhost:3000`? Do you get the same error if you use [Vercel CLI](https://vercel.com/docs/cli/build#vercel-build) to run `vercel build` or `vercel build --debug` on your local repo? 3den (@3den) · 2024-07-23 · ♥ 2 The problem on my end was coz the `<Link>` started all prefetching including prefetching the logout button, that it was the same logic since forever but vercel started default prefetch to true. All good now Planify La (@planify-la) · 2024-07-24 We tried because you mentioned it BUT it worked for couple of hours, now we have glitches with cookies again. Pauline P. Narvas (@pawlean) · 2024-07-24 @planify-la Do you have any error logs that could be helpful for us to look at? Planify La (@planify-la) · 2024-07-24 We identified that the error was because Nextjs was prefetching a logout link, we fixed it setting prefetch=false. The part that we don't understand is that the code has been working for months, and stopped working yesterday. Amy Egan (@amyegan) · 2024-07-24 It may have been the result of a recent package update. Does your repo have all packages locked to specific versions like `"some-package": "1.2.3"`, or are minor version updates allowed like `"some-package": "^1.2.3"`? Planify La (@planify-la) · 2024-07-25 We had ^14.2.4 Next.js version. Do you think this is related? In our package lock was 14.2.4. Yuno212 (@yuno212) · 2024-07-28 · ♥ 1 Hello if you have some prisma logic inside your app, do not forget to build your app with prisma generate in the build command, I've had some Failed collect page data too and I resolved it by building my app with prisma generate (maybe its a whole other problem but dont forget to add prisma generate if you've forgot to do so) Anish developer (@md0686888) · 2025-01-01 Hi there, The error indicates that the issue is likely with your environment variables or server-side execution in production. Here are some steps to resolve it: 1. **Check Environment Variables**: - Your `NEXTAUTH_URL` in production should point to your **Vercel deployment URL**, not `http://localhost:3000`. Update it in your Vercel environment settings: ```bash NEXTAUTH_URL=https://your-vercel-app-url.vercel.app ``` 2. **Validate Prisma Configuration**: - Ensure your `DATABASE_URL` is correct and accessible from the production environment. Test the connection locally to rule out any database-related issues. 3. **Build-Specific Errors**: - Run `npm run build` locally to confirm there are no errors in the build process. 4. **Update Dependencies**: - Ensure you’re using the latest versions of `next-auth`, `@next-auth/prisma-adapter`, and `bcrypt`. 5. **Debugging in Vercel**: - Add detailed logging in your API route (`api/auth/[...nextauth]/route.ts`) to identify what’s failing. For example: ```typescript console.log('Environment:', process.env); console.log('AuthOptions:', authOptions); ``` 6. **Secret Key Issue**: - Verify `NEXTAUTH_SECRET` is the same in both `.env` and Vercel.