2

Hi Please anyone can help me? I'm using a package

"next": "13.2.4",
"next-auth": "^4.20.1",
"react": "18.2.0",

and I have submit function to register the user

 async function onSubmit(values: any) {
    const options = {
      method: "POST",
      headers: { "Content-Type": "application/json", },
      body: JSON.stringify(values),
    }

    await fetch('/api/auth/signup', options)
      .then(res => res.json())
      .then((data) => {
        if (data) router.push('/authentication');
      });
    console.log(values)

  }

but when I hit submit I got this error

http://localhost:3000/api/auth/signup 404 (Not Found)

Below is my project directory

enter image description here

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
erwin
  • 99
  • 2
  • 9
  • API end-points should be declared inside the `pages` folder, so change your structure to `pages/api/auth`. Also, you should update `[...nextauth]` extension to `[...nextauth].ts`. – lpizzinidev Mar 31 '23 at 06:49
  • Also check your signup function, if there is something wrong it could cause 404 error – Lotpite Mar 31 '23 at 06:53
  • Nextjs 13 App Level Directory doesn't have Pages Directory anymore... – erwin Mar 31 '23 at 10:31

1 Answers1

1

the name of the file should be route.ts inside the directory. you are sending a request to /api/auth/signup. so you should have this folder structure

 -api
   -auth
     -signup
        -route.ts
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • Hi Yilmaz I got this error ```./node_modules/bson/lib/bson.mjs Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it) Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)``` – erwin Apr 01 '23 at 02:40
  • btw I did this config as well but no chance.. **experiments.topLevelAwait: true** – erwin Apr 01 '23 at 02:41
  • https://stackoverflow.com/questions/68339243/how-can-i-use-top-level-await-in-typescript-next-js/68339259#68339259 – Yilmaz Apr 01 '23 at 02:43
  • 1
    @erwin somewhere in your codebase you are using `await` which is not inside an `async` function – Yilmaz Apr 01 '23 at 02:45