Next.js Developer interview question · Most asked
How do you implement authentication in a Next.js App Router app?
Summary
Use a library such as Auth.js (NextAuth v5), Clerk, Better Auth or a custom session system: store sessions in HttpOnly cookies, read the session in Server Components/Actions/Route Handlers via cookies() or the library's auth(), and protect routes with server-side checks (optionally with middleware redirects).
Longer Answer
Centralize authorization in a data access layer (DAL) that verifies the session before returning data, so every page and action goes through it. Don't rely only on hiding UI or on middleware. Use secure, SameSite cookies, and for credentials auth hash passwords properly.
Key Points
- Session in HttpOnly cookies
- Check auth in server code near data access
- Data Access Layer pattern
- Middleware only for coarse redirects
More most-asked Next.js Developer questions
- How did caching defaults change in Next.js 15?
- What happens, step by step, when a request reaches a Next.js App Router application?
- How does data fetching work in the App Router?
- What are the differences between the App Router and the Pages Router?
- What is the difference between Server Components and Client Components?
- Explain the App Router file conventions: page, layout, loading, error, not-found, template and route.
- What is Next.js and what problems does it solve on top of React?
- Explain SSR, SSG, ISR and CSR in Next.js.