This is an example project that using Next.js with NextAuth to provide custom authorization with role based protected routes via Next.js middleware.
First, install dependencies:
yarn install
Run the development server:
yarn dev
Open http://localhost:3000 with your browser where you will see the home page with four routes you can go to: Sign In, Sign Out, User, Admin.
Sign In
page will allow you to authenticate the user and create session with chosen role.
Sign Out
page will clear up the session.
User
page can only access someone who has USER
role. Otherwise it's redirected to the Unauthorized
page.
Admin
page can only access someone who has ADMIN
role. Otherwise it's redirected to the Unauthorized
page.