NextAuth.js v5 Authentication

VerifiedSafe

Configure NextAuth.js v5 (Auth.js) authentication with OAuth providers (Google, Discord, GitHub) and Prisma adapter. Use to set up login/logout, protect routes, access user sessions in components, and manage session persistence with database strategy. Helps when adding OAuth providers or troubleshooting authentication issues.

Sby Skills Guide Bot
SecurityIntermediate
6/2/2026
Claude Code
#nextauth#authentication#oauth#prisma-adapter#session-management

Recommended for

Automated summary

This skill configures NextAuth.js v5 with OAuth providers (Google, Discord, GitHub) and a Prisma adapter for session persistence.

Strengths

  • Simplifies OAuth authentication integration with multiple providers.
  • Manages sessions in a database via Prisma, ensuring reliable persistence.
  • Easy route protection and client-side session access with SessionProvider.

Limitations

  • Requires a specific Prisma schema and database for database session mode.
  • Manual environment variable setup for each OAuth provider.
  • Does not cover non-OAuth authentication strategies (email, credentials, etc.).
When to use it

Use this skill when you need to set up OAuth authentication with multiple providers and database-backed sessions via Prisma in a Next.js application.

When not to use it

Avoid this skill if you are not using Next.js or prefer a simpler authentication strategy without a database (JWT only).

Security analysis

Safe
Quality score88/100

This skill is purely informational, providing code snippets and configuration instructions for integrating NextAuth.js. It contains no executable commands beyond standard package installation and secret generation, and it does not instruct any destructive, exfiltrating, or obfuscated actions. All environment variables are placeholders requiring user input.

No concerns found

How this analysis is made

Examples

Configure NextAuth v5 with Google and Prisma
Set up NextAuth.js v5 with Google OAuth provider and a Prisma adapter for database sessions in a Next.js app. Include the API route, session provider, and required Prisma schema.
Add Discord and GitHub OAuth providers
Extend the existing NextAuth configuration to also support Discord and GitHub OAuth providers, and update the Prisma schema if needed.
Protect API routes with NextAuth
Show me how to protect Next.js API routes using the auth() helper from NextAuth v5, returning a 401 response if the user is not authenticated.

name: nextauth description: >- Configure NextAuth.js v5 authentication with OAuth providers and Prisma adapter. Use when setting up login/logout, protecting routes, accessing sessions in components, adding OAuth providers, or troubleshooting authentication issues. license: MIT compatibility: [Claude Code] metadata: author: ftcmetrics version: "1.0.0" category: auth

NextAuth.js v5 Authentication Guide

NextAuth.js v5 (Auth.js) is the authentication library for FTC Metrics. It uses OAuth providers (Google, Discord, GitHub) with a Prisma database adapter for session persistence.

Quick Start

1. Install Dependencies

bun add next-auth@beta @auth/prisma-adapter

2. Create Auth Configuration

Create src/lib/auth.ts:

import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import Google from "next-auth/providers/google";
import Discord from "next-auth/providers/discord";
import GitHub from "next-auth/providers/github";
import { prisma } from "@ftcmetrics/db";

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    Google({
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
    }),
    Discord({
      clientId: process.env.DISCORD_CLIENT_ID!,
      clientSecret: process.env.DISCORD_CLIENT_SECRET!,
    }),
    GitHub({
      clientId: process.env.GITHUB_CLIENT_ID!,
      clientSecret: process.env.GITHUB_CLIENT_SECRET!,
    }),
  ],
  pages: {
    signIn: "/login",
    error: "/login",
  },
  callbacks: {
    async session({ session, user }) {
      if (session.user) {
        session.user.id = user.id;
      }
      return session;
    },
  },
  session: {
    strategy: "database",
  },
});

3. Create API Route Handler

Create src/app/api/auth/[...nextauth]/route.ts:

import { handlers } from "@/lib/auth";

export const { GET, POST } = handlers;

4. Add Session Provider

Create src/components/providers.tsx:

"use client";

import { SessionProvider } from "next-auth/react";

export function Providers({ children }: { children: React.ReactNode }) {
  return <SessionProvider>{children}</SessionProvider>;
}

Wrap your app in src/app/layout.tsx:

import { Providers } from "@/components/providers";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

Prisma Schema Requirements

Required models for NextAuth with database sessions:

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime? @map("email_verified")
  image         String?
  createdAt     DateTime  @default(now()) @map("created_at")
  updatedAt     DateTime  @updatedAt @map("updated_at")

  accounts Account[]
  sessions Session[]

  @@map("users")
}

model Account {
  id                String  @id @default(cuid())
  userId            String  @map("user_id")
  type              String
  provider          String
  providerAccountId String  @map("provider_account_id")
  refresh_token     String? @db.Text
  access_token      String? @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.Text
  session_state     String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
  @@map("accounts")
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique @map("session_token")
  userId       String   @map("user_id")
  expires      DateTime

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("sessions")
}

Environment Variables

Add to .env:

# NextAuth
AUTH_SECRET="generate-with-openssl-rand-base64-32"

# Google OAuth
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"

# Discord OAuth
DISCORD_CLIENT_ID="your-discord-client-id"
DISCORD_CLIENT_SECRET="your-discord-client-secret"

# GitHub OAuth
GITHUB_CLIENT_ID="your-github-client-id"
GITHUB_CLIENT_SECRET="your-github-client-secret"

Generate AUTH_SECRET:

openssl rand -base64 32

Session Management Patterns

Server Components (Recommended)

Use the auth() function directly in Server Components:

import { auth } from "@/lib/auth";
import { redirect } from "next/navigation";

export default async function DashboardPage() {
  const session = await auth();

  if (!session?.user) {
    redirect("/login");
  }

  return <div>Welcome, {session.user.name}</div>;
}

Protected Layouts

Protect entire route groups with layout-level auth:

import { auth } from "@/lib/auth";
import { redirect } from "next/navigation";

export default async function ProtectedLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  const session = await auth();

  if (!session?.user) {
    redirect("/login");
  }

  return <>{children}</>;
}

Client Components

Use the useSession hook in Client Components:

"use client";

import { useSession, signOut } from "next-auth/react";

export function UserMenu() {
  const { data: session, status } = useSession();

  if (status === "loading") {
    return <div>Loading...</div>;
  }

  if (!session?.user) {
    return <a href="/login">Sign in</a>;
  }

  return (
    <div>
      <img src={session.user.image} alt={session.user.name} />
      <span>{session.user.name}</span>
      <button onClick={() => signOut({ callbackUrl: "/" })}>
        Sign out
      </button>
    </div>
  );
}

Sign In with Providers

"use client";

import { signIn } from "next-auth/react";

export function LoginButtons() {
  return (
    <div>
      <button onClick={() => signIn("google", { callbackUrl: "/dashboard" })}>
        Continue with Google
      </button>
      <button onClick={() => signIn("discord", { callbackUrl: "/dashboard" })}>
        Continue with Discord
      </button>
      <button onClick={() => signIn("github", { callbackUrl: "/dashboard" })}>
        Continue with GitHub
      </button>
    </div>
  );
}

Adding User ID to Session

The session callback extends the session with the database user ID:

callbacks: {
  async session({ session, user }) {
    if (session.user) {
      session.user.id = user.id;
    }
    return session;
  },
},

Access the user ID in components:

// Server Component
const session = await auth();
const userId = session?.user?.id;

// Client Component
const { data: session } = useSession();
const userId = session?.user?.id;

OAuth Provider Setup

Google

  1. Go to Google Cloud Console
  2. Create OAuth 2.0 credentials
  3. Add authorized redirect URI: http://localhost:3000/api/auth/callback/google

Discord

  1. Go to Discord Developer Portal
  2. Create application and OAuth2 credentials
  3. Add redirect URI: http://localhost:3000/api/auth/callback/discord

GitHub

  1. Go to GitHub Developer Settings
  2. Create OAuth App
  3. Add callback URL: http://localhost:3000/api/auth/callback/github

Common Pitfalls

Session Not Available

  • Ensure SessionProvider wraps your entire app in the root layout
  • Ensure the API route exists at app/api/auth/[...nextauth]/route.ts

User ID Missing from Session

  • Add the session callback to include user.id
  • When using database strategy, user info comes from the user parameter, not token

Database Session Issues

  • Ensure Prisma schema matches NextAuth requirements exactly
  • Use onDelete: Cascade on relations to handle user deletion
  • Run prisma migrate dev after schema changes

OAuth Callback Errors

  • Verify redirect URIs match exactly in provider console
  • Include the full path: /api/auth/callback/[provider]
  • For production, update URIs to use HTTPS and your domain

AUTH_SECRET Missing

  • Generate with openssl rand -base64 32
  • Required in production, auto-generated in development

TypeScript Type Extensions

Extend session types in src/types/next-auth.d.ts:

import { DefaultSession } from "next-auth";

declare module "next-auth" {
  interface Session {
    user: {
      id: string;
    } & DefaultSession["user"];
  }
}

File Structure Reference

packages/web/src/
  lib/
    auth.ts                           # NextAuth configuration
  components/
    providers.tsx                     # SessionProvider wrapper
  app/
    api/auth/[...nextauth]/
      route.ts                        # API route handler
    layout.tsx                        # Root layout with Providers
    login/
      page.tsx                        # Login page (server)
      login-form.tsx                  # OAuth buttons (client)
    dashboard/
      layout.tsx                      # Protected layout
      page.tsx                        # Uses auth() for session

References

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/nextauth-js-v5-authentication-mm5t89yx/download' --output '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md.part' && mv '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md.part' '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md' && test -s '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/nextauth' && cp -R '.skill-downloads/nextauth-js-v5-authentication-mm5t89yx' '.claude/skills/nextauth'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/nextauth-js-v5-authentication-mm5t89yx'

openskills read 'nextauth-js-v5-authentication-mm5t89yx'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/nextauth-js-v5-authentication-mm5t89yx/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills