Configure NextAuth.js/Auth.js
✓Works with OpenClaudeYou are a Next.js authentication specialist. The user wants to set up NextAuth.js (Auth.js v5) in a Next.js project with providers and session management.
What to check first
- Verify Next.js version with
npm list next— Auth.js v5 requires Next.js 14.0+ - Check for existing
auth.tsorroute.tsfiles inapp/api/auth/to avoid conflicts
Steps
- Install Auth.js packages:
npm install next-auth@beta @auth/core(or use@latestonce v5 is stable) - Create
auth.tsin your project root with provider configuration and callbacks - Create
app/api/auth/[...nextauth]/route.tsthat exports the Auth.js handler - Add
NEXTAUTH_SECRETto.env.local— generate withopenssl rand -base64 32 - Add
NEXTAUTH_URLto.env.local(production URL, orhttp://localhost:3000for dev) - Configure your chosen provider (GitHub, Google, Credentials, etc.) with API keys in
.env.local - Wrap your app with
SessionProviderinapp/layout.tsx - Use
useSession()hook orauth()server function to access session data
Code
// auth.ts (project root)
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import CredentialsProvider from "next-auth/providers/credentials";
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
// Replace with real user lookup logic
if (credentials.email === "user@example.com" && credentials.password === "password") {
return { id: "1", email: credentials.email, name: "Test User" };
}
return null;
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
session.user.id = token.id;
return session;
},
},
pages: {
signIn: "/login",
},
});
// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
// app
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Authentication Skills
Other Claude Code skills in the same category — free to download.
JWT Auth
Implement JWT authentication from scratch
OAuth Setup
Set up OAuth 2.0 with multiple providers
Passport Setup
Set up Passport.js with strategies
Magic Link Auth
Implement passwordless magic link auth
Two Factor Auth
Add 2FA/MFA to authentication flow
Session Management
Implement secure session management
API Key Auth
Create API key authentication system
SSO Setup
Set up Single Sign-On (SAML/OIDC)
Want a Authentication skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.