Nextauth not working, getting a [CLIENT_FETCH_ERROR]
Answered
Maine Coon posted this in #help-forum
Maine CoonOP
I'm new to Next.js and Nextauth so bare with me if my terminology is not 100%
I'm trying to implement Nextauth and specifically auth with google.
I'm getting this error:
Some googling didn't really come up with good answers to fix things.
from my app/api/auth/[...nextauth].js:
from calling sign in my GoogleAuthButton.js
Can anyone help let me know what I'm doing wrong?
I'm trying to implement Nextauth and specifically auth with google.
I'm getting this error:
[next-auth][error][CLIENT_FETCH_ERROR]
https://next-auth.js.org/errors#client_fetch_error Unexpected token '<', "<!DOCTYPE "... is not valid JSON {error: {…}, url: '/api/auth/session', message: `Unexpected token '<', "<!DOCTYPE "... is not valid JSON`}
Some googling didn't really come up with good answers to fix things.
from my app/api/auth/[...nextauth].js:
export const authConfig = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
]
}
const handler = NextAuth(authConfig);
export { handler as GET, handler as POST};
from calling sign in my GoogleAuthButton.js
function GoogleAuthButton() {
const { data: session } = useSession();
const router = useRouter();
const handleSignIn = async () => {
try {
await signIn("google");
router.push("/signup");
} catch (error) {
console.error(error);
}
}
return (
<div>
{!session ? (
<button onClick={() => handleSignIn()}>Sign in with Google</button>
) : (
<>
<p>Welcome, {session.user.name}!</p>
<button onClick={() => signOut()}>Sign out</button>
</>
)}
</div>
);
};
export default GoogleAuthButton;
Can anyone help let me know what I'm doing wrong?
Answered by not-milo.tsx
You should export your next-auth handler from
/app/api/auth/[...nextauth]/route.ts
as shown here: https://next-auth.js.org/configuration/initialization#route-handlers-app12 Replies
Hello,
I get the same error in production when using the latest version of next and next-auth, but the error is somehow gone when using these versions:
I get the same error in production when using the latest version of next and next-auth, but the error is somehow gone when using these versions:
"next": "13.4.3",
"next-auth": "4.22.1",
I updated
I get the error when using a version of
next-auth
to the most recent version(4.23.1)
, and tried changing the next versions until I get the error again. I get the error when using a version of
next
greater than (13.4.12)
.The error happens only in production. Is that the case with you too?
You should export your next-auth handler from
/app/api/auth/[...nextauth]/route.ts
as shown here: https://next-auth.js.org/configuration/initialization#route-handlers-appAnswer
If you don't do this then your auth api routes won't be generated correctly and instead you'll get a 404 page. That's why you're seeing
unexpected token '<...
, because you're getting back html and not json data.Isn`t he doing it right here?
const handler = NextAuth(authConfig);
export { handler as GET, handler as POST};
oh sorry, his path is wrong.
Yeah
But my path is exactly like here /app/api/auth/[...nextauth]/route.ts and i still get the same error
I don´t receive the error only when I'm using versions i mentioned above
Your error might be of a different nature. Without seeing your code I can only guess.
For now let's see if changing the path solves this issue, and if it does you're free to open a new forum post with your specific problem.
For now let's see if changing the path solves this issue, and if it does you're free to open a new forum post with your specific problem.
Okay thank you. I hope it helps OP.