Labsco
auth0 logo

auth0-nextjs

β˜… 37

by auth0 Β· part of auth0/agent-skills

Add Auth0 authentication to Next.js apps with session management, protected routes, and middleware. Supports both App Router and Pages Router with automatic OAuth callback handling via /auth/login , /auth/logout , and /auth/callback endpoints Provides server-side session access through auth0.getSession() for Server Components and API routes, plus client-side useUser() hook for client components Requires middleware configuration ( middleware.ts for Next.js 15+, or proxy.ts for Next.js 16) to...

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup
🧩 One of 7 skills in the auth0/agent-skills package β€” works on its own, and pairs well with its siblings.

Add Auth0 authentication to Next.js apps with session management, protected routes, and middleware. Supports both App Router and Pages Router with automatic OAuth callback handling via /auth/login , /auth/logout , and /auth/callback endpoints Provides server-side session access through auth0.getSession() for Server Components and API routes, plus client-side useUser() hook for client components Requires middleware configuration ( middleware.ts for Next.js 15+, or proxy.ts for Next.js 16) to...

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates β€” shown here so you can audit it before installing. You don't need to read it to use the skill.

by auth0

Add Auth0 authentication to Next.js apps with session management, protected routes, and middleware. Supports both App Router and Pages Router with automatic OAuth callback handling via /auth/login , /auth/logout , and /auth/callback endpoints Provides server-side session access through auth0.getSession() for Server Components and API routes, plus client-side useUser() hook for client components Requires middleware configuration ( middleware.ts for Next.js 15+, or proxy.ts for Next.js 16) to... npx skills add https://github.com/auth0/agent-skills --skill auth0-nextjs Download ZIPGitHub37

Auth0 Next.js Integration

Add authentication to Next.js applications using @auth0/nextjs-auth0. Supports both App Router and Pages Router.

When NOT to Use

  • Client-side only React apps - Use auth0-react for Vite/CRA SPAs

  • React Native mobile apps - Use auth0-react-native for iOS/Android

  • Non-Next.js frameworks - Use framework-specific SDKs (Express, Vue, Angular, etc.)

  • Stateless APIs only - Use JWT validation middleware if you don't need session management

Welcome, {user.name}!

Logout

); }

return Login ; }

Copy & paste β€” that's it

### 6. Test Authentication

 Start your dev server:

npm run dev

Copy & paste β€” that's it

 Visit `http://localhost:3000` and test the login flow.

## Detailed Documentation

- **[Setup Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/setup.md)** - Automated setup scripts, environment configuration, Auth0 CLI usage 

- **[Integration Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/integration.md)** - Server-side auth, protected routes, API routes, middleware 

- **[API Reference](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/api.md)** - Complete SDK API, hooks, helpers, session management

## Related Skills

- `auth0-quickstart` - Basic Auth0 setup 

- `auth0-migration` - Migrate from another auth provider 

- `auth0-mfa` - Add Multi-Factor Authentication 

- `auth0-cli` - Manage Auth0 resources from the terminal

## Quick Reference

**V4 Setup:**

 

- Detect `src/` convention: check if `src/app/` or `src/pages/` exists β€” place all files inside `src/` if so 

- Create `lib/auth0.ts` (or `src/lib/auth0.ts`) with `Auth0Client` instance 

- Create middleware configuration (required):
 

- Next.js 15: `middleware.ts` (or `src/middleware.ts`) with `middleware()` function 

- Next.js 16: `middleware.ts` with `middleware()` OR `proxy.ts` with `proxy()` function (same `src/` rules) 

 

- Optional: Wrap with `<Auth0Provider>` for SSR user 

 **Client-Side Hooks:**

 

- `useUser()` - Get user in client components 

- `user` - User profile object 

- `isLoading` - Loading state 

 **Server-Side Methods:**

 

- `auth0.getSession()` - Get session in Server Components/API routes/middleware 

- `auth0.getAccessToken()` - Get access token for calling APIs 

 **Common Use Cases:**

 

- Login/Logout links β†’ Use `/auth/login` and `/auth/logout` paths (see Step 5) 

- Protected pages (App Router) β†’ [Integration Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/integration.md#protected-pages-app-router) 

- Protected pages (Pages Router) β†’ [Integration Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/integration.md#protected-pages-pages-router) 

- API routes with auth β†’ [Integration Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/integration.md#protected-api-routes) 

- Middleware protection β†’ [Integration Guide](https://github.com/auth0/agent-skills/blob/main/plugins/auth0/skills/auth0-nextjs/references/integration.md#middleware)

## References

- [Auth0 Next.js SDK Documentation](https://auth0.com/docs/libraries/nextjs) 

- [Auth0 Next.js Quickstart](https://auth0.com/docs/quickstart/webapp/nextjs) 

- [SDK GitHub Repository](https://github.com/auth0/nextjs-auth0)