Next-Auth Error - TypeError: fetch failed at Object.fetch
Asked Answered
C

1

6

I am having this issue that I cannot find the solution within any other forum.

My Setup

NextJS: v13.0.3

NextAuth: v4.16.4

npm: v8.19.2

node: v18.12.1

When the error occurs

This error happens ONLY in production. Within a development environment, everything works well. Once I press the button to go to the sign in page, the error I get is below. Signing in, does not show that I am logged in, nor redirects to the dashboard page. Only when I switch to the development environment, does it actually show that I am signed in. I do not get any errors within the browser, the only error I get is the one you see below. Additionally, visiting http://localhost:3000/api/auth/session, displays my login information correctly.

The Error

[next-auth][error][CLIENT_FETCH_ERROR] 
https://next-auth.js.org/errors#client_fetch_error fetch failed {
  error: {
    message: 'fetch failed',
    stack: 'TypeError: fetch failed\n' +
      '    at Object.fetch (node:internal/deps/undici/undici:11118:11)\n' +
      '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
    name: 'TypeError'
  },
  url: 'http://localhost:3000/api/auth/session',
  message: 'fetch failed'
}

Sign In Code

{allProviders.map((provider, index) => {
  const { id, name, Icon } = provider
    return (
      <div key={index}>
        <button
          aria-label="sign in"
          className="inline-flex items-center w-max px-5 py-3 rounded-lg bg-slate-100 gap-4"
          onClick={(e) => {
            e.preventDefault()
            signIn(id, { callbackUrl: '/dashboard' })
          }}
        >
          <Icon size={24} />
          {name}
       </button>
     </div>
   )
})}

.env file

NEXTAUTH_SECRET=******
NEXTAUTH_URL=http://localhost:3000

[...nextauth].js file

providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID as string,
      clientSecret: process.env.GITHUB_SECRET as string,
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_ID as string,
      clientSecret: process.env.GOOGLE_SECRET as string,
    }),
  ],
  pages: {
    signIn: '/auth/signin',
  },
  secret: process.env.NEXTAUTH_SECRET as string,
  callbacks: {
    async jwt({ token, account }: any) {
      if (account?.access_token) {
        token.access_token = account.access_token
      }
      return token
    },
    async session({ session, token }: any) {
      if (session?.user) {
        session.user.id = token.sub
      }
      return session
    },
  },

What I tried and DID NOT work:

  • Adding NEXT_PUBLIC before the env variables.
  • Removed the auth folder and had just the signin.tsx.
  • Added e.preventDefault() prior to signIn() execution.
  • Removed all the callbacks within [..nextauth].ts.
Coakley answered 18/11, 2022 at 4:32 Comment(5)
Did you figure this out? I'm running into the same issue, except only in my CI/CD Cypress tests. Super difficult to debug.Buffybuford
Ah, I doubt this solves your particular problem, but for me, I had to set NEXTAUTH_URL=http://127.0.0.1:3000. NEXTAUTH_URL=http://localhost:3000 did not workBuffybuford
Using NEXTAUTH_URL_INTERNAL with http://localhost:3000 worked for meUnsnarl
@MichaelHays I did not figure out how to solve it but for me the issue was that I was using getServerSideProps in the same file as the dashboard. So when I logged in, it didnt redirect me. I removed the getServerSideProps from my dashboard.tsx and it worked and the error wasn't there anymore.Coakley
@Unsnarl Hopefully, this works for others that come across this issue. Thank you though! Maybe this would help Michael?Coakley
S
-1

Update package.json, it will work fine

Snuffy answered 15/12, 2023 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.