I am in need of the nextjs middleware to be activated for two distinct paths:
- Firstly, to serve as a protection against unauthorized access by utilizing next-auth.
- Secondly, to redirect authorized users from specific pages. For example, if an authorized user lands on any page within the
auth
namespace, then they should be redirected to the dashboard. (Although I can achieve this on the client side, I find it more streamlined at the middleware level).
The issue is that it seems like next-auth takes control of the entire middleware logic:
import { withAuth } from "next-auth/middleware"
export default withAuth(
function middleware(req) {
console.log(req.nextauth.token)
},
{
callbacks: {
authorized: ({ token }) => token?.role === "admin",
},
}
)
export const config = { matcher: ["/dashboard/:path*", "/auth/:path*"] };
Is there a way to transfer this logic to a standard nextjs middleware? If not, how can I implement unrelated logic alongside next-auth?
Email: [email protected]