Overview
Implements secure authentication flows (login, register, forgot password, reset, logout, session refresh) using modern best practices: httpOnly cookies for tokens (never localStorage), PKCE for OAuth, CSRF protection, refresh token rotation, and clear guidance on JWT vs session vs OAuth tradeoffs. Provides complete flows for NextAuth.js / Auth.js, custom JWT, and session-based approaches.
When to Use This Skill
- Adding authentication to a new or existing web application.
- User requests "login page", "sign up", "protected route", "OAuth", or "session management".
- You need to choose between JWT, sessions, or OAuth and implement it securely.
Prerequisites
- Backend framework (Next.js, Express, etc.).
- Database or user store (Prisma, Drizzle, or auth provider).
- For OAuth: provider credentials (Google, GitHub, etc.).
- HTTPS in production (mandatory for cookies and OAuth).
Steps
Choose auth strategy:
- Sessions + httpOnly cookie: Simplest, good for traditional apps.
- JWT in httpOnly cookie + refresh rotation: Stateless, good for APIs and microservices.
- OAuth 2.1 + PKCE: For third-party login (recommended when possible).
- Compare pros/cons in the output.
Secure token storage:
- Always httpOnly + SameSite=Strict/Lax + Secure cookies.
- Never store tokens in localStorage or sessionStorage.
Implement flows:
- Login: validate credentials → issue tokens/cookie → redirect.
- Register: validate, hash password (bcrypt/argon2), create user.
- Password reset: time-limited token via email, one-time use.
- Refresh: short-lived access token + rotating refresh token.
- Logout: clear cookies, invalidate refresh token on server.
CSRF protection: Use SameSite + double-submit cookie or framework built-in.
Protected routes / middleware:
- Next.js middleware example that checks for valid session/JWT.
- Redirect unauthenticated users to /login with return URL.
NextAuth.js / Auth.js integration (recommended for most Next.js apps):
- Full config with credentials provider + Google/GitHub.
- Custom pages.
- Session callback to enrich user object.
Output:
- Complete auth service functions.
- Route handlers or API routes for each flow.
- Middleware.
- Frontend login/register forms (or point to form-validator skill).
- Environment variable list.
- Security checklist.
Examples
Full NextAuth config, custom JWT login/register handlers with httpOnly cookies, password reset flow with email token, and protected route middleware are included.
Edge Cases & Error Handling
- Invalid/expired refresh token: Force re-login, clear all cookies.
- Brute force: Rate limit login attempts per email/IP.
- Email verification: Optional but recommended step after registration.
- Account linking: For OAuth + credentials.
- Session fixation: Regenerate session on login.
Verification
- Register a new user.
- Login — verify httpOnly cookie is set (inspect in DevTools → Application → Cookies).
- Access a protected page while logged out — redirected to login.
- Refresh the page while logged in — session persists.
- Logout — cookie cleared.
- Attempt password reset end-to-end.
- Success: All flows work, no tokens in localStorage, cookies are httpOnly and secure-flagged in production.
References
1---2name: auth-flow-builder3description: Implements authentication flows including login, registration, password reset, and session management. Use when adding auth to a web app using JWT, sessions, or OAuth.4license: Apache-2.05compatibility: Claude Code, Codex, Gemini CLI6metadata:7 author: ai-skills8 version: "1.0"9 category: web-development10 tags: auth, jwt, oauth, sessions, nextauth, security, cookies11---1213## Overview1415Implements secure authentication flows (login, register, forgot password, reset, logout, session refresh) using modern best practices: httpOnly cookies for tokens (never localStorage), PKCE for OAuth, CSRF protection, refresh token rotation, and clear guidance on JWT vs session vs OAuth tradeoffs. Provides complete flows for NextAuth.js / Auth.js, custom JWT, and session-based approaches.1617## When to Use This Skill1819- Adding authentication to a new or existing web application.20- User requests "login page", "sign up", "protected route", "OAuth", or "session management".21- You need to choose between JWT, sessions, or OAuth and implement it securely.2223## Prerequisites2425- Backend framework (Next.js, Express, etc.).26- Database or user store (Prisma, Drizzle, or auth provider).27- For OAuth: provider credentials (Google, GitHub, etc.).28- HTTPS in production (mandatory for cookies and OAuth).2930## Steps31321. **Choose auth strategy**:33 - **Sessions + httpOnly cookie**: Simplest, good for traditional apps.34 - **JWT in httpOnly cookie + refresh rotation**: Stateless, good for APIs and microservices.35 - **OAuth 2.1 + PKCE**: For third-party login (recommended when possible).36 - Compare pros/cons in the output.37382. **Secure token storage**:39 - Always httpOnly + SameSite=Strict/Lax + Secure cookies.40 - Never store tokens in localStorage or sessionStorage.41423. **Implement flows**:43 - Login: validate credentials → issue tokens/cookie → redirect.44 - Register: validate, hash password (bcrypt/argon2), create user.45 - Password reset: time-limited token via email, one-time use.46 - Refresh: short-lived access token + rotating refresh token.47 - Logout: clear cookies, invalidate refresh token on server.48494. **CSRF protection**: Use SameSite + double-submit cookie or framework built-in.50515. **Protected routes / middleware**:52 - Next.js middleware example that checks for valid session/JWT.53 - Redirect unauthenticated users to /login with return URL.54556. **NextAuth.js / Auth.js integration** (recommended for most Next.js apps):56 - Full config with credentials provider + Google/GitHub.57 - Custom pages.58 - Session callback to enrich user object.59607. **Output**:61 - Complete auth service functions.62 - Route handlers or API routes for each flow.63 - Middleware.64 - Frontend login/register forms (or point to form-validator skill).65 - Environment variable list.66 - Security checklist.6768## Examples6970Full NextAuth config, custom JWT login/register handlers with httpOnly cookies, password reset flow with email token, and protected route middleware are included.7172## Edge Cases & Error Handling7374- **Invalid/expired refresh token**: Force re-login, clear all cookies.75- **Brute force**: Rate limit login attempts per email/IP.76- **Email verification**: Optional but recommended step after registration.77- **Account linking**: For OAuth + credentials.78- **Session fixation**: Regenerate session on login.7980## Verification81821. Register a new user.832. Login — verify httpOnly cookie is set (inspect in DevTools → Application → Cookies).843. Access a protected page while logged out — redirected to login.854. Refresh the page while logged in — session persists.865. Logout — cookie cleared.876. Attempt password reset end-to-end.887. Success: All flows work, no tokens in localStorage, cookies are httpOnly and secure-flagged in production.8990## References9192- [Auth.js / NextAuth.js Docs](https://authjs.dev/)93- [OWASP Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)94- [RFC 6749 OAuth 2.0 + PKCE (RFC 7636)](https://datatracker.ietf.org/doc/html/rfc7636)95- [HTTP Cookie Security](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies)