← Next.js learning pathNext.js · Module 8 of 10
Authentication and middleware
Protect pages and APIs with sessions and middleware.
📖 Key ideas
- Auth libraries: Auth.js (NextAuth), Clerk, Firebase Auth, Supabase Auth.
- Store sessions in secure httpOnly cookies.
- Middleware (middleware.ts / proxy) runs before requests to redirect or rewrite.
- Check auth again in server components, actions and route handlers.
- Never trust client-only checks.
💡 Real-life example: Middleware is a gatekeeper at the colony gate; each house (page) should still lock its own door.
Auth.jsAuthentication library for Next.js
MiddlewareCode that runs before a request is handled
SessionServer-known login state
RedirectSend the user to another URL