Skip to main content
skill CX Codex

authentication-patterns — Claude Code skill: guide and install

This skill equips coding agents with auth architecture guides, provider comparisons, and a pre-deployment security checklist. Use it when designing secure login flows.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add zebbern/claude-code-guide --skill authentication-patterns -a claude-code
Open on GitHub

What authentication-patterns does

The skill outlines trade-offs between database sessions, stateless JWTs, OAuth 2.0 / OIDC, and Passkeys. It details library choices across NextAuth, Clerk, Supabase Auth, and Lucia, including a sample Prisma adapter route for Next.js apps.

Access tokens must expire within 15 minutes. Storing auth tokens in localStorage or sessionStorage exposes users to XSS attacks, so the guide mandates httpOnly, Secure, and SameSite=Lax cookies. It enforces refresh token rotation where every token is single-use, backed by a server-side revocation blocklist.

The security checklist covers pre-ship requirements: rate-limiting logins to 5–10 attempts per minute per IP, CSRF defenses, and password hashing using argon2id or bcrypt at cost 12+.

What it helps with

  • choose between database sessions, JWTs, OAuth, and Passkeys
  • configure refresh token rotation with secure cookie storage
  • audit login endpoints against brute-force and credential stuffing
  • configure argon2id or bcrypt password hashing

Example prompts for the agent

  • Review my login endpoint against the authentication-patterns checklist
  • Compare session-based auth with JWT for my mobile API
  • Configure refresh token rotation using httpOnly secure cookies

How to install authentication-patterns

1Skills CLI (Vercel)

npx skills add zebbern/claude-code-guide --skill authentication-patterns -a claude-code

2Manually: put the files in place

~/.claude/skills/authentication-patterns/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/authentication-patterns/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about authentication-patterns

How do I install authentication-patterns in Claude Code?

Run npx skills add zebbern/claude-code-guide --skill authentication-patterns -a claude-code. The skill file will be placed in ~/.claude/skills/authentication-patterns/SKILL.md.

What does authentication-patterns do?

It provides architecture patterns for sessions, JWT, and OAuth, provider selection guides, and an authentication security checklist.

Where does the skill require storing auth tokens?

Tokens must be stored in httpOnly, Secure, SameSite=Lax cookies. The skill strictly advises against using localStorage or sessionStorage.

Source text

SKILL.md · MIT
---
name: authentication-patterns
description: "Authentication patterns: session vs JWT vs OAuth comparison, provider selection (NextAuth, Clerk, Supabase Auth), security checklist, and common mistakes. Use when implementing auth, reviewing auth flows, or choosing auth providers."
---

# Authentication Patterns Skill

Reference for implementing secure, production-ready authentication.

## WHEN_TO_USE

Apply this skill when implementing authentication in a project, reviewing existing auth flows for security issues, choosing between auth providers, or migrating between auth strategies. Use the security checklist before shipping any auth-related change.

## AUTH_APPROACHES

| Approach | How It Works | Best For | Drawbacks |
|----------|-------------|----------|-----------|
| Session-based | Server stores session in DB/Redis, client holds session ID cookie | Traditional server-rendered apps, apps needing instant revocation | Requires server-side storage, harder to scale horizontally without shared store |
| JWT (stateless) | Server signs token, client sends it on each request | API-first apps, microservices, mobile clients | Cannot revoke without blocklist, token size grows with claims |
| OAuth 2.0 / OIDC | Delegates auth to external provider (Google, GitHub, etc.) | Social login, enterprise SSO, reducing auth responsibility | More complex flow, depends on external provider availability |
| Passkeys / WebAuthn | Cryptographic key pair, no passwords | High-security apps, passwordless UX | Limited browser support legacy, user education needed |

### Decision Guide

- **Server-rendered app with simple needs** → Session-based
- **SPA or mobile app calling APIs** → JWT with refresh token rotation
- **Want social login or SSO** → OAuth 2.0 / OIDC
- **Greenfield with modern UX goals** → Passkeys + OAuth fallback

## JWT_BEST_PRACTICES

### Token Lifecycle

```
Login → Access Token (short-lived) + Refresh Token (long-lived, rotated)
  │
  ├─ Access Token: 15 min expiry, sent via httpOnly cookie or Authorization header
  │
  └─ Refresh Token: 7-30 day expiry, stored in httpOnly secure cookie
       │
       └─ On use: issue new access + new refresh token, invalidate old refresh token
```

### Rules

- [P0-MUST] Set short expiry on access tokens (15 minutes or less).
- [P0-MUST] Store tokens in `httpOnly`, `Secure`, `SameSite=Lax` cookies — never in `localStorage` or `sessionStorage`.
- [P0-MUST] Implement refresh token rotation — each refresh token is single-use.
- [P0-MUST] Maintain a server-side blocklist for revoked refresh tokens.
- [P1-SHOULD] Include only essential claims in JWT payload (sub, iat, exp, role). Keep it small.
- [P1-SHOULD] Use asymmetric signing (RS256 or ES256) for distributed systems; symmetric (HS256) for single-service only.
- [P1-SHOULD] Validate `iss`, `aud`, and `exp` claims on every request.
- [P2-MAY] Use JWE (encrypted JWT) when token payload contains sensitive data.

### Token Storage Comparison

| Storage | XSS Safe | CSRF Safe | Recommendation |
|---------|----------|-----------|----------------|
| `httpOnly` cookie | Yes | No (needs CSRF token) | Recommended |
| `localStorage` | No | Yes | Never use for auth tokens |
| `sessionStorage` | No | Yes | Never use for auth tokens |
| In-memory (JS variable) | Yes | Yes | OK for SPAs, lost on refresh |

## PROVIDER_PATTERNS

### Comparison

| Provider | Type | Best For | Pricing | Key Features |
|----------|------|----------|---------|-------------|
| NextAuth / Auth.js | OSS library | Next.js apps wanting full control | Free | 80+ providers, DB adapters, self-hosted |
| Clerk | Managed service | Fast launch, pre-built UI, user management | Free tier, then per-MAU | Drop-in components, user dashboard, org support |
| Supabase Auth | Managed (part of Supabase) | Apps already using Supabase for DB/storage | Free tier, then per-MAU | Row-level security integration, magic links, SSO |
| Lucia | OSS library | Full control, minimal abstraction | Free | Session-based, framework-agnostic, type-safe |

### When to Use Each

- **NextAuth / Auth.js**: You want provider flexibility, self-hosting, and database session control. Best when you need custom flows.
- **Clerk**: You want auth done fast with pre-built UI components. Best for MVPs and teams that don't want to build auth UI.
- **Supabase Auth**: You're already using Supabase. Auth integrates with RLS policies for row-level security.
- **Lucia**: You want a minimal, type-safe session library without framework lock-in.

### NextAuth.js Setup Pattern

```typescript
// app/api/auth/[...nextauth]/route.ts
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/lib/prisma";

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: PrismaAdapter(prisma),
  providers: [
    GitHub({
      clientId: process.env.GITHUB_CLIENT_ID!,
      clientSecret: process.env.GITHUB_CLIENT_SECRET!,
    }),
  ],
  callbacks: {
    session({ session, user }) {
      session.user.id = user.id;
      return session;
    },
  },
});
```

## SECURITY_CHECKLIST

### Before Shipping Auth

- [ ] **Rate limiting**: Login endpoint limited to 5-10 attempts per minute per IP.
- [ ] **CSRF protection**: Anti-CSRF tokens on all state-changing requests (or use `SameSite=Lax` cookies).
- [ ] **Password hashing**: Using bcrypt (cost 12+) or argon2id — never MD5, SHA-1, or plain SHA-256.
- [ ] **HTTPS only**: All auth endpoints served over TLS. Cookies have `Secure` flag.
- [ ] **Input validation**: Email format, password length (min 8, max 128), no SQL/NoSQL injection vectors.
- [ ] **Account enumeration**: Login and registration return the same response whether account exists or not.
- [ ] **Session invalidation**: Logout invalidates server-side session/refresh token, not just client cookie.
- [ ] **MFA support**: TOTP (authenticator app) or WebAuthn as second factor for sensitive accounts.
- [ ] **Password reset**: Time-limited tokens (1 hour), single-use, sent over secure channel.
- [ ] **Audit logging**: Log auth events (login, logout, failed attempts, password changes) with timestamp and IP.

### Password Hashing

```typescript
// Using bcrypt
import bcrypt from "bcrypt";

const SALT_ROUNDS = 12;

async function hashPassword(password: string): Promise<string> {
  return bcrypt.hash(password, SALT_ROUNDS);
}

async function verifyPassword(password: string, hash: string): Promise<boolean> {
  return bcrypt.compare(password, hash);
}
```

```typescript
// Using argon2 (preferred for new projects)
import argon2 from "argon2";

async function hashPassword(password: string): Promise<string> {
  return argon2.hash(password, { type: argon2.argon2id });
}

async function verifyPassword(hash: string, password: string): Promise<boolean> {
  return argon2.verify(hash, password);
}
```

## COMMON_MISTAKES

| Mistake | Risk | Fix |
|---------|------|-----|
| Storing JWT in `localStorage` | XSS can steal tokens | Use `httpOnly` cookies |
| Long-lived JWTs (days/weeks) | Stolen token is valid for extended period | 15 min access token + refresh rotation |
| Missing CSRF protection | Attackers can forge requests from other sites | `SameSite=Lax` cookies + CSRF token |
| Weak password requirements | Brute force and credential stuffing | Min 8 chars, check against breached password lists |
| Exposing user existence on login | Account enumeration | Generic "Invalid credentials" message |
| Not rotating refresh tokens | Stolen refresh token grants indefinite access | Single-use refresh tokens with rotation |
| Hardcoding secrets in source | Credential leak via git history | Use environment variables, never commit secrets |
| Missing rate limiting on login | Brute force attacks | 5-10 attempts/min per IP, exponential backoff |
| Rolling your own crypto | Subtle vulnerabilities | Use established libraries (bcrypt, argon2, jose) |
| Not validating JWT claims | Token misuse across services | Always verify `iss`, `aud`, `exp` |

© zebbern · MIT · zebbern/claude-code-guide

Similar skills

All skills for Security →

From the same set40

code-documenter A skill that writes docstrings, builds OpenAPI specs and documentation sites. Useful when the code works but nobody documented it. CX Codex +1 ★ 4,679repo stars chart-image Renders publication-quality PNG charts in Node.js without Puppeteer or Chromium. Useful for headless servers, alerts, and automated reports. CX Codex +1 ★ 4,679repo stars caching Use this skill to plan or review caching, from TTL choices and cache keys to invalidation. It fits work on API, service, and client-side caches. CX Codex +1 ★ 4,679repo stars aws-penetration-testing A skill that walks the agent through AWS pentesting: IAM enumeration, privilege escalation, metadata SSRF, S3 and Lambda exploitation. CX Codex +1 ★ 4,679repo stars audit-flow Trace paths through CODE, API, AUTH, DATA, and NETWORK, keep audit state in SQLite, and export diagrams. Useful for security reviews, debugging, and architecture analysis. CX Codex +1 ★ 4,679repo stars api-shape-explorer Runs several sub-agents at once so you get radically different interface designs for a module and can compare them before writing code. CX Codex +1 ★ 4,679repo stars active-directory-attacks This skill provides reference workflows and tool syntax for Active Directory penetration testing. It helps agents guide domain enumeration, Kerberos attacks, and privilege escalation. CX Codex +1 ★ 4,679repo stars academic-paper-reviewer This skill simulates peer review of a research paper, assessing originality, methods, results, and writing. Use it to get structured revision feedback. CX Codex +1 ★ 4,679repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor