Next.js Discord

Discord Forum

NextAuth

Unanswered
Lancashire Heeler posted this in #help-forum
Open in Discord
Lancashire HeelerOP
trying to get role in middleware using NextAuth but failed.

import { withAuth } from "next-auth/middleware"

export default withAuth({
  callbacks: {
    authorized: ({ req, token }) => {


      console.log(JSON.stringify(token?.role))


      const path = req.nextUrl.pathname;

      // Check if the middleware is processing the
      // route which requires a specific role
      if (path.startsWith("/admin")) {
        return token?.role === "admin";
      }

      // By default return true only if the token is not null
      // (this forces the users to be signed in to access the page)
      return token !== null;
    }
  }
})

// Define paths for which the middleware will run
export const config = {
  matcher: [
    "/:path*",
  ]
}

1 Reply

Spotted Rail
@Lancashire Heeler are u facing any error? if yes then post it or provide some more context