nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
22.87k stars 3.08k forks source link

next auth on vercel its showing me server error. "There is a problem with the server configuration. Check the server logs for more information." #10930

Closed OlegTernovuy closed 4 weeks ago

OlegTernovuy commented 4 weeks ago

Environment

System:
    OS: macOS 14.0
    CPU: (8) arm64 Apple M1
    Memory: 66.27 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.5.0 - ~/node_modules/.bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.6.7 - /usr/local/bin/npm
  Browsers:
    Safari: 17.0
  npmPackages:
    next: 14.0.4 => 14.0.4 
    next-auth: ^4.24.7 => 4.24.7 
    react: ^18 => 18.2.0 

Reproduction URL

https://github.com/OlegTernovuy/TechStop/tree/Setting-NextAuth

Describe the issue

I work with NextJS, next-auth and use NestJS with ready-made routes as a backend API. When I run it locally, everything works with login and registration, but when I run it on versel, 3 errors are thrown in the console when rendering the page.

Знімок екрана 2024-05-15 о 22 15 01

On the forums, in almost all similar cases, the error says "no-secret-key", but I just get "There is a problem with the server configuration". Then the question arises whether it is necessary to use this secret-key if all actions take place on the backend and whether it will help?

export const authOptions: AuthOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: {
          label: "Username",
          type: "text",
          placeholder: "jsmith",
        },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        if (!credentials?.email || !credentials?.password) return null;
        const { email, password } = credentials;
        const res = await fetch(BASE_URL + "/auth/login", {
          method: "POST",
          body: JSON.stringify({
            email,
            password,
          }),
          headers: {
            "Content-Type": "application/json",
          },
        });
        if (res.status === 200) {
          try {
            const user = await res.json();
            return user.data;
          } catch (error) {
            console.error("Error parsing response:", error);
            return null;
          }
        } else {
          try {
            const errorResponse = await res.json();
            return { error: errorResponse.message };
          } catch (error) {
            console.error("Error parsing error response:", error);
          }
          return null;
        }
      },
    }),
  ],

  callbacks: {
    async signIn({ user }) {
      if (user?.error) throw new Error(user?.error);

      return true;
    },
    async jwt({ token, user }) {
      if (user) {
        // return { ...token, ...user };
        token.user = user;
        token.token = token.token;
      }
      return token;
    },

    async session({ token, session }) {
      session.user = token.user;
      session.token = token.token;

      return session;
    },
  },
};

How to reproduce

Visit https://tech-stop-qgyz-git-setting-nextauth-olegternovuys-projects.vercel.app?_vercel_share=nAwxJaEhZBFT7s7VODGyvgeQT1CxmlBn

Expected behavior

Login and registration should work

kevinmitch14 commented 4 weeks ago

Can you check Vercel logs? Usually it will give you a bit more context.

OlegTernovuy commented 4 weeks ago

Just add secret_key