Next JS 14 Server action on vercel causes 405 error

724 views Asked by At

I have deployed a project on Vercel using Next 14 and the new server actions rather than use an API. The actions work fine when running locally can log in fine but when I deploy to vercel it does not work. It gives an error 405 (Method not allowed). I read online from other posts that others have fixed it by removing "generateStaticParams" but I do not use that anywhere within my code. I'm confused as to why it works locally but not on vercel.

Form state for login form

const [state, formAction] = useFormState(authenticate, undefined);

Authenticate function

export const authenticate = async (prevState, formData) => {
  const { username, password } = Object.fromEntries(formData);

  try {
    await signIn("credentials", { username, password });
  } catch (err) {
    return "Wrong Credentials!";
  }
};

signIn function is next-auth. it is connected to a login function seen below

CredentialsProvider({
      async authorize(credentials) {
        try {
          const user = await login(credentials);
          return user;
        } catch (err) {
          return null;
        }
      },
    }),

the login function

const login = async (credentials) => {
  try {
    connectToDB();
    const user = await User.findOne({ email: credentials.username });

    if (!user) throw new Error('Wrong credentials!');

    const isPasswordCorrect = await bcrypt.compare(
      credentials.password,
      user.password
    );

    if (!isPasswordCorrect) throw new Error('Wrong credentials!');

    return user;
  } catch (err) {
    console.log(err);
    throw new Error('Failed to login!');
  }
};

Full error:

POST https://www.domain_name/login net::ERR_ABORTED 405 (Method Not 
Allowed)
fetchServerAction @ 472-95aa8650790068a5.js:1
await in fetchServerAction (async)
serverActionReducer @ 472-95aa8650790068a5.js:1
clientReducer @ 472-95aa8650790068a5.js:1
(anonymous) @ 472-95aa8650790068a5.js:1
eg @ fd9d1056-ab87c093a14536cd.js:9
dg @ fd9d1056-ab87c093a14536cd.js:9
n.useReducer @ 472-95aa8650790068a5.js:17
useReducerWithReduxDevtoolsImpl @ 472-95aa8650790068a5.js:1
Router @ 472-95aa8650790068a5.js:1
Mf @ fd9d1056-ab87c093a14536cd.js:9
Bh @ fd9d1056-ab87c093a14536cd.js:9
rC @ fd9d1056-ab87c093a14536cd.js:9
Jk @ fd9d1056-ab87c093a14536cd.js:9
Mk @ fd9d1056-ab87c093a14536cd.js:9
tk @ fd9d1056-ab87c093a14536cd.js:9
qf @ fd9d1056-ab87c093a14536cd.js:9
P @ 472-95aa8650790068a5.js:25
0

There are 0 answers