Next.js Discord

Discord Forum

NextJS withAuth middleware

Unanswered
Least Tern posted this in #help-forum
Open in Discord
Least TernOP
How can I redirect user to a certen page instead of returning false?
import { withAuth } from "next-auth/middleware";

export default withAuth({
  callbacks: {
    authorized({ req, token }) {
      // `/admin` requires admin role
      if (
        req.nextUrl.pathname === "/moj-profil/podesavanje-profila" &&
        token?.name &&
        token?.picture
      ) {
        return false;
      }
      if (
        req.nextUrl.pathname === "/moj-profil" &&
        !token?.name &&
        !token?.picture
      ) {
        return false;
      }
      // `/me` only requires the user to be logged in
      return !!token;
    },
  },
});

export const config = {
  matcher: [
    "/moj-profil/podesavanje-profila",
    "/moj-profil/",
    "/pregled-zakazanih",
    "/api/radnja",
  ],
};

0 Replies