#nextjs routing/authorization 404

4 messages · Page 1 of 1 (latest)

glad osprey
#

Using Convex Auth with nextjs.
Signin form on /en/sign-in route calling signIn function from useAuthActions() on submit which triggers fetch on /api/auth endpoint. Server responses with 404, and in verbose convex auth log shows
[ConvexAuthNextjs] Not proxying auth action to Convex, path /en/sign-in does not match /api/auth how to debug/resolve this issue?

wind yewBOT
#

Hi @glad osprey. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The 404 error when calling /api/auth in your Next.js app with Convex Auth is a common issue, usually related to routing or middleware configuration. Here’s how to debug and resolve it:

1. Ensure /api/auth Route Exists and Is Handled by Middleware

Convex Auth for Next.js expects authentication actions (like signIn from useAuthActions()) to be handled at the /api/auth endpoint. The middleware must be set up to proxy these requests to Convex. If the middleware is missing, misconfigured, or not matching the route, you’ll see the log:

[ConvexAuthNextjs] Not proxying auth action to Convex, path /en/sign-in does not match /api/auth

This means the request to /en/sign-in is not being proxied, but your sign-in form should be triggering a fetch to /api/auth, not /en/sign-in. Double-check that your sign-in form is not submitting to the page route, but is instead calling the signIn function, which should POST to /api/auth via the Convex Auth client.

2. Middleware Placement and Matcher

Your middleware.ts (or middleware.js) file must be in the correct location. If you’re using a src directory, the middleware file must also be inside src (e.g., src/middleware.ts). If it’s not, Next.js won’t run it, and /api/auth won’t be handled, resulting in a 404. This is a common pitfall:

I needed to move the middleware file into the src folder as well, after doing so it seems to be working as intended.
Discord support thread: Convex auth failing when adding src directory to project

3. Middleware Matcher Configuration

Ensure your middleware’s matcher includes /api/auth:

#
export const config = {
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

This ensures that requests to /api/auth are intercepted and handled by Convex Auth middleware, which is required for authentication actions to work properly. If /api/auth is not matched, you’ll get a 404 from Next.js, not Convex.

4. Verbose Logging

Enable verbose logging in your middleware to get more insight:

export default convexAuthNextjsMiddleware(optionalHandler, {
  verbose: true,
});

This will help you see exactly what the middleware is doing and why it might not be proxying requests as expected.
See: Debugging Convex Auth

5. Common Mistakes

  • Wrong environment variables: Make sure NEXT_PUBLIC_CONVEX_URL is set correctly.
  • Typos in middleware: Even a small typo can prevent the middleware from working (Discord thread).
  • Not running npx convex dev: The backend must be running and up-to-date (Discord thread).

Summary of steps to resolve:

  • Ensure your middleware is in the correct directory (src if you use it).
  • Confirm your middleware’s matcher includes /api/auth.
  • Enable verbose logging for more details.
  • Double-check your sign-in form is not submitting to the page route.
  • Make sure your Convex backend is running.

If you follow these steps, the 404 on /api/auth should be resolved and Convex Auth will proxy authentication actions correctly through your Next.js app.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: