This demo project showcases role-based authentication and authorization using Next.js 15 (App Router) and Auth0. It demonstrates how to implement protected routes, admin-only pages, and secured API endpoints.
/api/public
)/api/admin
) - requires admin roleVisit Auth0 Signup to create an account for FREE!
git clone <repository-url>
cd next-auth0-rbac-demo
npm install
Create an Auth0 application and API, then set up the following environment variables in .env.local
:
AUTH0_SECRET='use [openssl rand -hex 32] to generate a 32 bytes value'
AUTH0_BASE_URL='http://localhost:3000'
AUTH0_ISSUER_BASE_URL='https://YOUR_AUTH0_DOMAIN'
AUTH0_CLIENT_ID='YOUR_CLIENT_ID'
AUTH0_CLIENT_SECRET='YOUR_CLIENT_SECRET'
AUTH0_DOMAIN='YOUR_AUTH0_DOMAIN'
In your Auth0 dashboard:
npm run dev
āāā app/
ā āāā api/ # API routes
ā ā āāā protected/ # Admin-only endpoints
ā ā āāā public/ # Public endpoints
ā āāā page.tsx # Home page component
ā āāā admin/ # Admin dashboard page
ā ā āāā page.tsx # Admin dashboard component
ā āāā logged-in/ # Logged-in user pages
ā āāā page.tsx # Logged-in user component
āāā actions/ # Server actions
āāā components/ # React components
āāā lib/ # Utility functions
āāā middleware.ts # Auth & CORS middleware
GET / api / public;
// Accessible to all users
GET / api / protected;
// Requires valid Auth0 token and admin role
š„ Want to earn $120k+/year as a developer? Our 1000+ students already are! Transform your career with the most comprehensive full-stack development program available!
Join Zero to Full Stack Hero 2.0 today!
š Complete Full-Stack Curriculum
šØāš« Live Coaching & Support
šŖ Real-World Experience
š¤ Active Developer Community
Join Zero to Full Stack Hero 2.0 Today!
"The best investment in my development career" - Frank Ramos, Harvard Graduate & Senior Developer
This project is part of the Zero to Full Stack Hero 2.0 curriculum, showcasing advanced authentication patterns with Next.js and Auth0.