[GH-ISSUE #6671] NextJs API route returns 404 Not found #19223

Closed
opened 2026-04-15 18:03:53 -05:00 by GiteaMirror · 12 comments
Owner

Originally created by @alexm92 on GitHub (Dec 10, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6671

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Set up NextJS 16 with better-auth 1.4.6
  2. Configure Better Auth with standard catch-all route:
// src/app/api/auth/[...all]/route.js
import { toNextJsHandler } from 'better-auth/next-js';
import { auth } from '@/lib/auth';

export const { GET, POST } = toNextJsHandler(auth);
  1. Configure Google auth
  2. Login component
'use client';

export function LoginForm() {
  return (
    <Button
      variant="outline"
      onClick={async () => {
        const { data, error } = await authClient.signIn.social({
          provider: 'google',
        });
        console.log('login google response', { data, error });
        // error is { status: 404, statusText: 'Not Found' }
      }}
    >
      Login with Google
    </Button>
  );
}
  1. Notice the login does not work and the request returns 404

Current vs. Expected behavior

Previously, I was using next-auth and everything worked, but better auth simply just doesn't want to.

I expect to be able to login.

What version of Better Auth are you using?

1.4.6

System info

Doesn't return anything

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { nextCookies } from 'better-auth/next-js';

export const auth = betterAuth({
  socialProviders: {
    google: {
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }
  },
  plugins: [
    nextCookies(),
  ]
});

Additional context

I'm testing only locally, not sure if it matters, but I have a custom domain in /etc/hosts e.g. https://local.example.com:3000

This is happening on:

  • macOS 15.4.1
  • Chrome 142.0.7444.176
Originally created by @alexm92 on GitHub (Dec 10, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6671 Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Set up NextJS 16 with better-auth 1.4.6 2. Configure Better Auth with standard catch-all route: ```js // src/app/api/auth/[...all]/route.js import { toNextJsHandler } from 'better-auth/next-js'; import { auth } from '@/lib/auth'; export const { GET, POST } = toNextJsHandler(auth); ``` 3. Configure Google auth 4. Login component ```js 'use client'; export function LoginForm() { return ( <Button variant="outline" onClick={async () => { const { data, error } = await authClient.signIn.social({ provider: 'google', }); console.log('login google response', { data, error }); // error is { status: 404, statusText: 'Not Found' } }} > Login with Google </Button> ); } ``` 5. Notice the login does not work and the request returns 404 ### Current vs. Expected behavior Previously, I was using next-auth and everything worked, but better auth simply just doesn't want to. I expect to be able to login. ### What version of Better Auth are you using? 1.4.6 ### System info ```bash Doesn't return anything ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { nextCookies } from 'better-auth/next-js'; export const auth = betterAuth({ socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, } }, plugins: [ nextCookies(), ] }); ``` ### Additional context I'm testing only locally, not sure if it matters, but I have a custom domain in `/etc/hosts` e.g. `https://local.example.com:3000` This is happening on: - macOS 15.4.1 - Chrome 142.0.7444.176
GiteaMirror added the locked label 2026-04-15 18:03:53 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 10, 2025):

It appears that the catch‐all API route isn’t being matched correctly by Next.js. Here are a few troubleshooting steps you can take:

• Verify that your route file is placed inside the app directory exactly as “src/app/api/auth/[...all]/route.js”. Any typo or folder misplacement can result in a 404 error. [1]

• Check that your Next.js version properly supports the new app directory routes. Since you mentioned you’re using Next.js 16, confirm that it fully supports your project’s file naming and route conventions. [1]

• Ensure that your custom domain (https://local.example.com:3000) is correctly configured in your environment (for instance, in BETTER_AUTH_URL if needed) and that it doesn’t interfere with Next.js routing. [2]

• Confirm that the Google auth configuration is correctly set up and that there are no issues with redirect URIs or callback paths that might cause the sign-in response to fail. [2]

If you’re still experiencing the issue, checking your server logs for any error messages during route matching could provide additional clues.

Tag @better-auth-agent if you would like an answer from the Agent. What other aspects of your setup could we look into?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3638231583 --> @better-auth-agent[bot] commented on GitHub (Dec 10, 2025): It appears that the catch‐all API route isn’t being matched correctly by Next.js. Here are a few troubleshooting steps you can take: • Verify that your route file is placed inside the app directory exactly as “src/app/api/auth/[...all]/route.js”. Any typo or folder misplacement can result in a 404 error. [[1]](https://github.com/better-auth/better-auth/issues/1183) • Check that your Next.js version properly supports the new app directory routes. Since you mentioned you’re using Next.js 16, confirm that it fully supports your project’s file naming and route conventions. [[1]](https://github.com/better-auth/better-auth/issues/1183) • Ensure that your custom domain (https://local.example.com:3000) is correctly configured in your environment (for instance, in BETTER_AUTH_URL if needed) and that it doesn’t interfere with Next.js routing. [[2]](https://github.com/better-auth/better-auth/issues/4250) • Confirm that the Google auth configuration is correctly set up and that there are no issues with redirect URIs or callback paths that might cause the sign-in response to fail. [[2]](https://github.com/better-auth/better-auth/issues/4250) If you’re still experiencing the issue, checking your server logs for any error messages during route matching could provide additional clues. Tag @better-auth-agent if you would like an answer from the Agent. What other aspects of your setup could we look into? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@alexm92 commented on GitHub (Dec 10, 2025):

The route works, I've hardcoded GET to return a dummy response, it just doesn't work through toNextJsHandler(auth)

<!-- gh-comment-id:3638250711 --> @alexm92 commented on GitHub (Dec 10, 2025): The route works, I've hardcoded GET to return a dummy response, it just doesn't work through `toNextJsHandler(auth)`
Author
Owner

@EthanKim88 commented on GitHub (Dec 10, 2025):

export const auth = betterAuth({
  baseURL: "https://local.example.com:3000", // Add this
  ....
});

is it work ?

<!-- gh-comment-id:3638455095 --> @EthanKim88 commented on GitHub (Dec 10, 2025): ```typescript export const auth = betterAuth({ baseURL: "https://local.example.com:3000", // Add this .... }); ``` is it work ?
Author
Owner

@alexm92 commented on GitHub (Dec 10, 2025):

Nope, I've tried setting both baseURL and the env BETTER_AUTH_URL

<!-- gh-comment-id:3638869233 --> @alexm92 commented on GitHub (Dec 10, 2025): Nope, I've tried setting both `baseURL` and the env `BETTER_AUTH_URL`
Author
Owner

@EthanKim88 commented on GitHub (Dec 10, 2025):

It would help if you could share:

  1. Your exact baseURL value (e.g., http://local.example.com:3000 or https://...)
  2. The URL that returns 404 (check the Network tab to see which request is failing)
  3. Your updated auth.ts with baseURL included (the code you shared earlier doesn't have it)

This will help others diagnose the issue more easily.

<!-- gh-comment-id:3638966519 --> @EthanKim88 commented on GitHub (Dec 10, 2025): It would help if you could share: 1. Your exact `baseURL` value (e.g., `http://local.example.com:3000` or `https://...`) 2. The URL that returns 404 (check the Network tab to see which request is failing) 3. Your updated `auth.ts` with `baseURL` included (the code you shared earlier doesn't have it) This will help others diagnose the issue more easily.
Author
Owner

@alex-bondarev-linnovate commented on GitHub (Dec 11, 2025):

We can also reproduce this. One thing to note is that it 404s with the trailingSlash NextJS setting. If we set it to false, the 404 goes away.

<!-- gh-comment-id:3641153491 --> @alex-bondarev-linnovate commented on GitHub (Dec 11, 2025): We can also reproduce this. One thing to note is that it 404s with the `trailingSlash` NextJS setting. If we set it to `false`, the 404 goes away.
Author
Owner

@alexm92 commented on GitHub (Dec 11, 2025):

@alex-bondarev-linnovate Right, this seems to be the reason for 404

<!-- gh-comment-id:3641223480 --> @alexm92 commented on GitHub (Dec 11, 2025): @alex-bondarev-linnovate Right, this seems to be the reason for 404
Author
Owner

@maxdevos49 commented on GitHub (Dec 31, 2025):

I made a PR in better-call which fixes url construction for toNodeHandler: https://github.com/better-auth/better-call/pull/93. I believe it will resolve this

<!-- gh-comment-id:3702748881 --> @maxdevos49 commented on GitHub (Dec 31, 2025): I made a PR in better-call which fixes url construction for toNodeHandler: https://github.com/better-auth/better-call/pull/93. I believe it will resolve this
Author
Owner

@bytaesu commented on GitHub (Jan 14, 2026):

Is this issue still happening? Let me check 🧐

<!-- gh-comment-id:3747268957 --> @bytaesu commented on GitHub (Jan 14, 2026): Is this issue still happening? Let me check 🧐
Author
Owner

@naveen-newstart25 commented on GitHub (Jan 14, 2026):

@bytaesu still happens

<!-- gh-comment-id:3747835101 --> @naveen-newstart25 commented on GitHub (Jan 14, 2026): @bytaesu still happens
Author
Owner

@bytaesu commented on GitHub (Jan 15, 2026):

Alright, I see what's happening. Let me think about how we can handle this!

<!-- gh-comment-id:3752655002 --> @bytaesu commented on GitHub (Jan 15, 2026): Alright, I see what's happening. Let me think about how we can handle this!
Author
Owner

@devnchill commented on GitHub (Jan 16, 2026):

i am also facing this issue T_T

<!-- gh-comment-id:3760786865 --> @devnchill commented on GitHub (Jan 16, 2026): i am also facing this issue T_T
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19223