'How do i fix an internal server error when authenticating with Next-Auth

I am trying to add authentication to my Next.js project with Next-Auth, however I am stuck on a 500 internal server error after submitting credentials (http://localhost:3000/api/auth/error?error=Configuration).

I think it might be something to do with running on http://localhost:3000 but I'm not sure. Can anyone see what i'm doing wrong?

pages/api/auth/[...nextauth].js

import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';

const options = {
  site: 'http://localhost:3000',

  providers: [
    Providers.Credentials({
      name: 'Credentials',
      credentials: {
        username: { label: 'Username', type: 'text', placeholder: 'jsmith' },
        password: { label: 'Password', type: 'password' },
      },
      authorize: async (credentials) => {
        consol.log('credentials', credentials);
        const user = { id: 1, name: 'J Smith', email: '[email protected]' };
        if (user) {
          return Promise.resolve(user);
        } else {
          return Promise.resolve(null);
        }
      },
    }),
  ],
  database: process.env.MONGO_URI,
};

export default (req, res) => NextAuth(req, res, options);

pages/index.js

import { useSession } from 'next-auth/client';

export default function Home() {
  const [session, loading] = useSession();
  console.log('session', session);

  return (
    <div className="container">
      <main>
          {session && <p>Signed in as {session.user.email}</p>}
          {!session && (
            <p>
              <a href="/api/auth/signin">Sign in</a>
            </p>
          )}
      </main>
    </div>
  );
}

pages/_app.js

import { Provider } from 'next-auth/client';
import '../styles.css';

export default ({ Component, pageProps }) => {
  const { session } = pageProps;
  return (
    <Provider options={{ site: process.env.SITE }} session={session}>
      <Component {...pageProps} />
    </Provider>
  );
};

next.config.js

module.exports = {
  env: {
    MONGO_URI: '...',
    SITE: 'http://localhost:3000',
  },
};

Any help would be really appreciated.



Solution 1:[1]

In your pages/api/auth/[...nextauth].js add:

secret:process.env.SECRET

SECRET must be any string value like this:

SECRET:LlKq6ZtYbr+hTC073mAmAh9/h2HwMfsFo4hrfCx6gts=

Also add it in your vercel env. Thats it, your problem will be solved.

Solution 2:[2]

You had a typo under your asyc function consol.log('credentials', credentials);

Fix to console.log('credentials', credentials); and that should solve the problem.

I faced a similar problem and it was because I didn't add any providers array.

In the future, fix any typos or errors within your [...nextauth].js to fix 500 error

Solution 3:[3]

As mentioned by @Dinesh, my problem was solved by adding a NEXTAUTH_SECRET to both Vercel Environment Variables and to [...nextauth].ts

Local deployment worked without that variable, but vercel deployment needed the secret. In addition to adding the Vercel environment variable, my working [...nextauth].ts looks like:

export default NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
  ],
  secret: process.env.NEXTAUTH_SECRET,
  callbacks: {
    async jwt({ token }) {
      token.userRole = "user"
      return token
    },
  },
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ian Campbell
Solution 2 ninsau
Solution 3 daliudzius