generated from fahricansecer/boilerplate-fe
Initial commit
This commit is contained in:
42
middleware.ts
Normal file
42
middleware.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user