0

I am getting 500 internal server error after deploying a Next.js app to Vercel. The project works in the local machine but isn't working in the URL to which it is deployed to. enter image description here

Error

I have used environment variables in Vercel, which might be related to the issue. I added these 4 env variables - NEXTAUTH_URL, NEXTAUTH_SECRET, TWITTER_CLIENT_ID, TWITTER_CLIENT_SECRET.

In my project, I've created a separate file '.env.local' which contains all of my project-related keys and URLs.

At first, this env variable 'NEXTAUTH_URL' was pointing to 'http://localhost:3000/'

NEXTAUTH_URL = http://localhost:3000/

And then, after deploying my app in Vercel, I updated that variable with the deployed URL in my project as well as in Vercel.

NEXTAUTH_URL = https://twitter-clone-seven-coral.vercel.app/

I have also added the above URL in 'Twitter's Developer Portal' in 'OAuth 2.0' in the 'Callback URI/Redirected URL' section:

enter image description here

Before deploying my app in Vercel, the CALLBACK URI/REDIRECT URL was pointing to https://localhost:3000/api/auth/callback/twitter
and WEBSITE URL was pointing to https://test.com which I then updated after deploying the app initially.

This is the first time I'm working with Environment variables, so I do not have much idea on how to proceed with this error.

Package.json for reference: enter image description here

2
  • Hi! A couple of questions first; Do you have any Nextjs api running ( in the /api folder in your project). If yes, your /api route could cause some collisions. Also, are the environmental variables that should be public, public? You can ensure that by adding NEXT_PUBLIC_ to the variable name. I have personally worked with the server side authentication and had an issure related to overriding the /api route, which i had to switch in the end for /backend to prevent name collisions Commented Jun 13, 2022 at 8:05
  • What do the Vercel logs show when the page returns the 500 error? Commented Jun 19, 2022 at 21:11

4 Answers 4

2

This is what I found about deploying to Vercel.

https://next-auth.js.org/deployment

make sure what you are doing is correct based on that link.

These are other post that I fount somewhat like this problem, mostly people saying you have to add "NEXTAUTH_SECRET" for it to run in production:

How to fix api/auth/error issue of next-auth in production?

https://github.com/nextauthjs/next-auth/issues/5268

But if you are implementing a Database adapter, that could be the problem.

In my case, I used Prisma and needed to add in the Prisma folder:

import { PrismaClient } from '@prisma/client';

let prisma: PrismaClient;

if (process.env.NODE_ENV === "production") {
  prisma = new PrismaClient();
} else {
  let globalWithPrisma = global as typeof globalThis & {
    prisma: PrismaClient;
  };
  if (!globalWithPrisma.prisma) {
    globalWithPrisma.prisma = new PrismaClient();
  }
  prisma = globalWithPrisma.prisma;
}

export default prisma;

and latter in the [...nextauth] file do:

import prisma from "../../../../../prisma/prisma";

//...

const handler = NextAuth({
  adapter: PrismaAdapter(prisma) as Adapter,
  //...
});

//...

I hope this helped.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, have to set the Environment Variable , tried it with vercel but didnot supported or maybe missed something, but works fine with netlify, just deploy with the environment variable, from your project .env.local files, get the keys and values give also provide the NEXTAUTH_URL properly , then it should run

Comments

0

I don't know for other hosting platforms and tiers but when I deployed my app to Vercel free tier I had to add every Environment Variable to the hosting platform.

You need to go to Dashboard -> Your app -> Settings -> Environment Variables and there you need to add all of your variables. With same name and value that you used in .env.local.

Also, don't forget to use NEXT_PUBLIC_ in front of every Environment Variable name. So it should look like this eg. NEXT_PUBLIC_MONGODB_URI="somethingsomething". So this NEXT_PUBLIC_ before the name of Environment Variable makes it so your Environment Variable is exposed to the browser. You don't need to use NEXT_PUBLIC_ when working localy but when you want to deploy then you need to use it in order to expose the Environment Variable to the browser.

I hope this helps. It helped me.

1 Comment

NEXT_PUBLIC should be added only for public keys, not for db or other sensitive keys. it will leak the keys!
-2

Had faced same problem and find out solution after a long research

The Trick is you have to set environment variable to vercel or any host platform

how to set environment variable in vercel

how to set environment variable in heroku

Comments

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.