Curriculum
We'll build a simple auth system called Authy to see how Next.js handles authentication. We'll use next-auth
which is rebranded to Auth.js to handle all the auth stuff, but more importantly, you'll learn about:
Here's the authentication system you'll be building:
Try admin/admin
credentials to log in.
This is a public page accessible to everyone.
You can find the Github repo for all the lessons here . The repo is divided into different commits for each lesson. As you follow along, you can check out the repo to see the final code for each lesson or if you get stuck.
Create a basic authentication system with the following routes:
/
- Public home page/login
- Login page/dashboard
- Protected dashboard page/dashboard/settings
- Protected settings page/dashboard/profile
- Protected profile pageImplement route protection using middleware
Create different layouts for authenticated and non-authenticated pages
Add dynamic navigation based on auth state
Handle "not found" and error pages
The app has a simple header with navigation links that change based on whether you're logged in or not. There's also a handy auth panel that shows your current path and login status - super useful for debugging!
For this demo, we're keeping it simple with admin/admin credentials (I know, I know - but hey, it's just for learning!). Once you log in, you'll see:
Try accessing /dashboard without logging in - you'll get bounced right back to the login page. That's route protection in action!