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 thesignin.tsx
. - Added
e.preventDefault()
prior to signIn() execution. - Removed all the
callbacks
within[..nextauth].ts
.
NEXTAUTH_URL=http://127.0.0.1:3000
.NEXTAUTH_URL=http://localhost:3000
did not work – BuffybufordNEXTAUTH_URL_INTERNAL
withhttp://localhost:3000
worked for me – Unsnarl