kojac
kojac5mo ago

api routes with next-intl

Hi, I'm using convex password auth with nextjs and it was working fine, but then i implemented next-intl and now the api routes are returning 404 i've tried to make the next-intl dont run on the api routes but then in also return an error on the next-intl saying that: Unable to find next-intl locale because the middleware didn't run on this request. See https://next-intl-docs.vercel.app/docs/routing/middleware#unable-to-find-locale. The notFound() function will be called as a result. POST /api/auth 404 in 171ms with next-intl running on the api route: POST /pt-BR/api/auth 404 in 39ms Middleware.ts:
import createIntlMiddleware from 'next-intl/middleware';
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
isAuthenticatedNextjs,
nextjsMiddlewareRedirect
} from "@convex-dev/auth/nextjs/server";
import { routing } from './i18n/routing';

const isPublicPage = createRouteMatcher(["/auth"]);

const intlMiddleware = createIntlMiddleware(routing);

export default async function middleware(request, event) {
// First, handle internationalization routing
const intlResponse = await intlMiddleware(request, event);
if (intlResponse) {
return intlResponse;
}

// Then, handle authentication routing
return convexAuthNextjsMiddleware((request, event) => {
if (!isPublicPage(request) && !isAuthenticatedNextjs()) {
return nextjsMiddlewareRedirect(request, "/auth");
}

if (isPublicPage(request) && isAuthenticatedNextjs()) {
return nextjsMiddlewareRedirect(request, "/");
}
})(request, event);
}

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)", "/(pt-BR|en)/:path*"],
};
import createIntlMiddleware from 'next-intl/middleware';
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
isAuthenticatedNextjs,
nextjsMiddlewareRedirect
} from "@convex-dev/auth/nextjs/server";
import { routing } from './i18n/routing';

const isPublicPage = createRouteMatcher(["/auth"]);

const intlMiddleware = createIntlMiddleware(routing);

export default async function middleware(request, event) {
// First, handle internationalization routing
const intlResponse = await intlMiddleware(request, event);
if (intlResponse) {
return intlResponse;
}

// Then, handle authentication routing
return convexAuthNextjsMiddleware((request, event) => {
if (!isPublicPage(request) && !isAuthenticatedNextjs()) {
return nextjsMiddlewareRedirect(request, "/auth");
}

if (isPublicPage(request) && isAuthenticatedNextjs()) {
return nextjsMiddlewareRedirect(request, "/");
}
})(request, event);
}

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)", "/(pt-BR|en)/:path*"],
};
ill be happy if somebody could give me some direction here, thanks in advance guys!
No description
1 Reply
sshader
sshader5mo ago
Have you tried skipping the middleware just for /api/auth as opposed to all of /api? I don't think anything in /api/auth should be requiring the locale.

Did you find this page helpful?