Next.js Discord

Discord Forum

next-auth invalid JSON in credentials

Answered
Lagotto Romagnolo posted this in #help-forum
Open in Discord
Lagotto RomagnoloOP
The below code is what i am using to authorize a user based on a return from an API call. the response should be { email: "email@email.com", password: "password123"}. But the code does not work and I get the error:
Unexpected token '<', "<!DOCTYPE "... is not valid JSON

export const authOptions = {
  providers: [
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "Email", type: "text" },
        password: { label: "Password", type: "password" },
      },
      async authorize(credentials, req) {
        const res = await fetch("http://localhost:3000/pages/api/user/signin", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            email: credentials?.email,
            password: credentials?.password,
          }),
        });

        const user = await res.json();

        if (user) {
          return user;
        }

        return null;
      },
    }),
  ],
};


but when I hard code the user as follows:
const user = {email: "email@email.com", and password: "password123"}
then perform this logic, the authorization works perfectly well.:
if (credentials?.email === user.email && credentials?.password == user.password) {
  return user;
}
return null;


How can I fix this?
Answered by Giant panda
You are receiving a 404 page as a response because your URL is wrong
View full answer

6 Replies

Giant panda
You are receiving a 404 page as a response because your URL is wrong
Answer
Giant panda
You are fetching /pages/api
Remove the pages prefix
Lagotto RomagnoloOP
wow lmao, you are my hero
my hairline receded another inch trying to figure this out
tysm