Auth.jsのv5-betaと、next-intlを組み合わせてmiddlewareを書く方法が載ってなかったのでメモを残します。
import { auth } from '@/auth';
import { routing } from '@/i18n/routing';
import createMiddleware from 'next-intl/middleware';
const handleI18nRouting = createMiddleware(routing);
export default auth(handleI18nRouting);
export const config = {
matcher: ['/((?!api|_next|.*\\..*).*)'],
};
なお、auth
は以下。詳しい書き方はこちら。
export const { handlers, signIn, signOut, auth } = NextAuth({
// ...
});
また、routing
は以下。詳しい書き方はこちら
import { createNavigation } from 'next-intl/navigation';
import { defineRouting } from 'next-intl/routing';
export const routing = defineRouting({
// A list of all locales that are supported
locales: ['en', 'ja'],
// Used when no locale matches
defaultLocale: 'ja',
});
export type Locale = (typeof routing.locales)[number];
// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const { Link, redirect, usePathname, useRouter, getPathname } =
createNavigation(routing);