generated from fahricansecer/boilerplate-fe
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { withAuth } from "next-auth/middleware";
|
|
import createMiddleware from "next-intl/middleware";
|
|
import { routing } from "./src/i18n/routing";
|
|
|
|
const intlMiddleware = createMiddleware(routing);
|
|
|
|
const publicPages = ["/signin", "/signup"];
|
|
|
|
const authMiddleware = withAuth(
|
|
// Note: It is important to return the intlMiddleware to handle locale rewrites/redirects
|
|
(req) => intlMiddleware(req),
|
|
{
|
|
callbacks: {
|
|
authorized: ({ token }) => token != null,
|
|
},
|
|
pages: {
|
|
signIn: "/signin",
|
|
},
|
|
}
|
|
);
|
|
|
|
export default function middleware(req: any) {
|
|
const publicPathnameRegex = RegExp(
|
|
`^(/(${routing.locales.join("|")}))?(${publicPages
|
|
.flatMap((p) => (p === "/" ? ["", "/"] : p))
|
|
.join("|")})/?$`,
|
|
"i"
|
|
);
|
|
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname);
|
|
|
|
if (isPublicPage) {
|
|
return intlMiddleware(req);
|
|
} else {
|
|
return (authMiddleware as any)(req);
|
|
}
|
|
}
|
|
|
|
export const config = {
|
|
// Skip all paths that should not be internationalized.
|
|
// This skips the folders "api", "_next" and all files with an extension (e.g. favicon.ico)
|
|
matcher: ["/((?!api|_next|.*\\..*).*)"],
|
|
};
|