# Auth with Auth.js (NextAuth)

> Add sessions, OAuth providers and credentials login to a Next.js app with the open-source Auth.js library and your own database. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/authjs
Last updated: 2026-11-28

Add sessions, OAuth providers and credentials login to a Next.js app with the open-source Auth.js library and your own database.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [OAuth 2.0, OpenID Connect and Social Login](https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login), [Server Actions and Mutations](https://www.internhack.in/roadmaps/tracks/full-stack/server-actions)

**Auth.js** (formerly NextAuth.js) is an open-source authentication library with first-class Next.js support. Your users and sessions live in your own database, and there is no per-user pricing.

### Setup

- Configure providers (Google, GitHub, email magic links, credentials) in one auth config file.
- Use the **Prisma or Drizzle adapter** to store users, accounts and sessions in PostgreSQL.
- Choose database sessions or JWT sessions in the config.

### Using it in the App Router

- Call auth() in server components, route handlers and server actions to get the current session.
- Protect routes in middleware by redirecting signed-out users to the login page.
- Use signIn and signOut helpers from client or server code.

### Extending

Add fields like role or collegeId to the session through callbacks, and keep authorization checks in your own service code.

### Pitfalls

- Relying on middleware alone for protection; always check the session again in server actions and handlers that change data.
- Exposing sensitive user fields in the session object sent to the client.
- Misconfigured callback URLs between local and production environments.

You are ready to move on when your Next.js portal supports Google login and email magic links, stores users in PostgreSQL, protects the dashboard, and exposes the user's role in the session.

## Key concepts

- **Providers configuration**
- **Database adapters**
- **auth() in server code**
- **Middleware route protection**
- **Session callbacks and roles**
- **Magic link email login**
- **Better Auth**: Another self-hosted TypeScript auth library

## Other options for this step

- [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk): Use a hosted identity service for sign-in UI, user management and MFA, and understand the trade-offs of outsourcing auth.

## Mini project

Add Auth.js to your Next.js placement portal with Google login and a Prisma adapter, protect /dashboard, add a STUDENT or RECRUITER role to the session, and show role-specific navigation.

## Check yourself

Middleware redirects signed-out users away from /dashboard. Why do your server actions still need to call auth()?

## Free resources

- [Auth.js](https://authjs.dev/) (Auth.js)
- [Better Auth](https://www.better-auth.com/docs) (Better Auth)

Previous: [OAuth 2.0, OpenID Connect and Social Login](https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login) · Next: [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk)

## More in Authentication, Authorization and Security

- [Authentication Fundamentals and Password Storage](https://www.internhack.in/roadmaps/tracks/full-stack/auth-fundamentals)
- [Sessions, Cookies and JWTs](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt)
- [OAuth 2.0, OpenID Connect and Social Login](https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login)
- [Auth with Auth.js (NextAuth)](https://www.internhack.in/roadmaps/tracks/full-stack/authjs)
- [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk)
- [Authorization and Access Control](https://www.internhack.in/roadmaps/tracks/full-stack/authorization-rbac)
- [Web Security Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/web-security)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
