Next.js Discord

Discord Forum

next auth + intl middleware

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
import { NextRequest } from "next/server";
import { withAuth } from "next-auth/middleware";
import createIntlMiddleware from "next-intl/middleware";

const locales = ["en", "ka"];
const authPages = ["/register", "/login"];

const intlMiddleware = createIntlMiddleware({
  locales,
  defaultLocale: "en",
});

const authMiddleware = withAuth((req) => intlMiddleware(req), {
  callbacks: {
    authorized: ({ token }) => token != null,
  },
  pages: {
    signIn: "/login",
  },
});
const authPagesMiddleware = withAuth((req) => intlMiddleware(req), {
  callbacks: {
    authorized: ({ token }) => token != null,
  },
  pages: {
    signIn: "/",
  },
});

export default function middleware(req: NextRequest) {
  const authPathnameRegex = RegExp(
    `^(/(${locales.join("|")}))?(${authPages.join("|")})?/?$`,
    "i"
  );
  const isAuthPage = authPathnameRegex.test(req.nextUrl.pathname);

  if (isAuthPage) {
    return (authPagesMiddleware as any)(req);
  } else {
    return (authMiddleware as any)(req);
  }
}

export const config = {
  // Skip all paths that should not be internationalized
  matcher: ["/((?!api|_next|.*\\..*).*)"],
};

9 Replies

Sun bearOP
i have /login and /register pages
when you signed in i want to redirect to main page if user go to these auth pages
otherwise if user is not signed in
he cannot see any page except these
i also wont to add two things
1. when user is blocked, he would go login page but signOut user as well
2. if user is admin he can access /admin and any other specific pages
this next intl next auth integration not working also