# Auth Patterns

> Implements secure authentication patterns. JWT with refresh tokens, OAuth 2.0, RBAC, session management. Use when building auth for any project. Don't use for social login setup.

- Skill: `neuron-one/auth-patterns` (Agent Skill)
- Install (CLI): `npx skillmds@latest add neuron-one/auth-patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/neuron-one/auth-patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- Author: neuron-one (https://skillmd.com/u/neuron-one)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/neuron-one/auth-patterns

---


# Secure Authentication Patterns

## JWT Implementation
- Access token: short-lived (15 min)
- Refresh token: long-lived (7 days), stored in httpOnly cookie
- Always validate: expiry, issuer, audience
- Never store JWT in localStorage (XSS vulnerable)
- Rotate refresh tokens on each use

## Password Security
- Hash with bcrypt (cost factor 12+)
- Minimum 8 characters, require complexity
- Rate limit login attempts (5 per minute)
- Account lockout after 10 failed attempts
- Never log passwords, even hashed

## RBAC (Role-Based Access Control)
- Define roles: admin, manager, user
- Middleware checks role on EVERY protected route
- Principle of least privilege
- Separate admin endpoints from user endpoints

## Session Management
- Regenerate session ID after login
- Expire inactive sessions (30 min)
- Allow user to see/revoke active sessions
- Invalidate all sessions on password change

## API Security
- CORS: whitelist specific origins, not *
- Rate limiting per user and per IP
- Input validation with schema (Zod, Joi)
- Sanitize all outputs
