Replies: 8 comments 9 replies
-
Did you find a solution ? |
Beta Was this translation helpful? Give feedback.
-
Did you find a solution? Also running into this |
Beta Was this translation helpful? Give feedback.
-
Actually I couldn't find any sloutions for this, to be honest I didn't try but I changed to clerck js authentication and it worked |
Beta Was this translation helpful? Give feedback.
-
How can I exclude dynamic routes from the middleware |
Beta Was this translation helpful? Give feedback.
-
This worked for me "next": "^14.1.0",
"react": "18.2.0",
"next-auth": "5.0.0-beta.13",
"next-intl": "^3.4.2", import type { NextRequest } from "next/server"
import createIntlMiddleware from "next-intl/middleware"
import { auth } from "@/auth"
import { defaultLocale, localePrefix, locales } from "./messages/config"
const publicPages = [
"/",
"/sign-in",
"/sign-up",
"/forgot-password",
"/reset-password",
"/unauthorized",
"/contact",
"/about",
"/help",
"/privacy-policy",
"/terms-of-service",
"/cookie-policy",
"/end-user-license-agreement"
]
const intlMiddleware = createIntlMiddleware({
locales,
defaultLocale,
localePrefix
})
const authMiddleware = auth((req) => {
// private routes here
const session = req.auth
if (session) {
return intlMiddleware(req)
}
})
export default function middleware(req: NextRequest) {
const publicPathnameRegex = RegExp(
`^(/(${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 = {
matcher: ["/((?!api|_next|.*\\..*).*)"]
} |
Beta Was this translation helpful? Give feedback.
-
Let nextAuth handle it first and then pass that request to I18nMiddlewareimport { getToken } from "next-auth/jwt";
import { withAuth } from "next-auth/middleware";
import { createI18nMiddleware } from "next-international/middleware";
const I18nMiddleware = createI18nMiddleware({
locales: ["en", "fa"],
defaultLocale: "fa",
});
const withAuthMiddleware = withAuth(
async function middleware(req) {
const token = await getToken({ req });
const isAuth = !!token;
const isAuthPage =
req.nextUrl.pathname.startsWith("/login") ||
req.nextUrl.pathname.startsWith("/register");
if (isAuthPage) {
if (isAuth) {
return I18nMiddleware(req);
// return NextResponse.redirect(new URL("/dashboard", req.url));
}
return null;
}
if (!isAuth) {
let from = req.nextUrl.pathname;
if (req.nextUrl.search) {
from += req.nextUrl.search;
}
return I18nMiddleware(req);
// return NextResponse.redirect(
// new URL(`/login?from=${encodeURIComponent(from)}`, req.url),
// );
}
},
{
callbacks: {
async authorized() {
// This is a work-around for handling redirect on auth pages.
// We return true here so that the middleware function above
// is always called.
return true;
},
},
},
);
export default withAuthMiddleware;
export const config = {
matcher: [
"/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt).*)",
],
}; |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, sorry for my english))) I tried to do this, i understand, that my code is not excellent, but it's working. I tested my code and i didn't find bags. But I will test this code in a future)
|
Beta Was this translation helpful? Give feedback.
-
this is pain |
Beta Was this translation helpful? Give feedback.
-
Hello,
I think in the Next Auth(V5) which is released recently they have changed a couple of things so the pervious workarounds does not really help in my knowledge. My middleware code is down below and i really don't know how to chain this two middleware. Can I request a reproduction of this code Thanks again
Beta Was this translation helpful? Give feedback.
All reactions