Installation
Installation
Section titled “Installation”Introduction
Section titled “Introduction”Setting up Auth.js in a Next.js project takes just a few steps. This guide covers installation, environment variables, and the basic project structure.
Installation Steps
Section titled “Installation Steps”1. Install the Package
Section titled “1. Install the Package”npm install next-authFor Auth.js v5 (beta) with the new App Router syntax:
npm install next-auth@beta2. Environment Variables
Section titled “2. Environment Variables”Create a .env file with your configuration:
# RequiredNEXTAUTH_URL=http://localhost:3000NEXTAUTH_SECRET=your-random-secret-here
# Only needed if using OAuth providersGITHUB_CLIENT_ID=your-github-client-idGITHUB_CLIENT_SECRET=your-github-client-secretGOOGLE_CLIENT_ID=your-google-client-idGOOGLE_CLIENT_SECRET=your-google-client-secretGenerate a strong secret:
openssl rand -base64 323. Create the Auth Route Handler
Section titled “3. Create the Auth Route Handler”import NextAuth from 'next-auth'import { authOptions } from '@/lib/auth'
const handler = NextAuth(authOptions)export { handler as GET, handler as POST }4. Create the Auth Configuration
Section titled “4. Create the Auth Configuration”import { NextAuthOptions } from 'next-auth'import GitHubProvider from 'next-auth/providers/github'
export const authOptions: NextAuthOptions = { providers: [ GitHubProvider({ clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET!, }), ], session: { strategy: 'jwt', },}5. Add SessionProvider (for client components)
Section titled “5. Add SessionProvider (for client components)”'use client'
import { SessionProvider } from 'next-auth/react'
export function Providers({ children }: { children: React.ReactNode }) { return <SessionProvider>{children}</SessionProvider>}import { Providers } from './providers'
export default function RootLayout({ children }) { return ( <html> <body> <Providers>{children}</Providers> </body> </html> )}Project Structure
Section titled “Project Structure”my-app/├── app/│ ├── api/│ │ └── auth/│ │ └── [...nextauth]/│ │ └── route.ts│ ├── providers.tsx│ └── layout.tsx├── lib/│ └── auth.ts└── .envCommon Mistakes
Section titled “Common Mistakes”- Forgetting
NEXTAUTH_SECRET— Without it, Auth.js generates a weak secret in development and throws an error in production. - Not wrapping the app in
SessionProvider—useSession()will fail in client components without it. - Missing environment variables for providers — OAuth providers will throw errors without their client ID and secret.
Best Practices
Section titled “Best Practices”- Use
openssl rand -base64 32to generate a strongNEXTAUTH_SECRET - Set
NEXTAUTH_URLto your production domain in deployment - Store provider secrets in environment variables, not in code
- Add
.envto.gitignoreto prevent committing secrets
Summary
Section titled “Summary”Auth.js installs with npm install next-auth. Configure providers, environment variables, and the route handler. Wrap your app in SessionProvider to use sessions in client components.