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.05---67## Overview89Implements 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.1011## When to Use This Skill1213- Adding authentication to a new or existing web application.14- User requests "login page", "sign up", "protected route", "OAuth", or "session management".15- You need to choose between JWT, sessions, or OAuth and implement it securely.1617## Prerequisites1819- Backend framework (Next.js, Express, etc.).20- Database or user store (Prisma, Drizzle, or auth provider).21- For OAuth: provider credentials (Google, GitHub, etc.).22- HTTPS in production (mandatory for cookies and OAuth).2324## Steps25261. **Choose auth strategy**:27 - **Sessions + httpOnly cookie**: Simplest, good for traditional apps.28 - **JWT in httpOnly cookie + refresh rotation**: Stateless, good for APIs and microservices.29 - **OAuth 2.1 + PKCE**: For third-party login (recommended when possible).30 - Compare pros/cons in the output.31322. **Secure token storage**:33 - Always httpOnly + SameSite=Strict/Lax + Secure cookies.34 - Never store tokens in localStorage or sessionStorage.35363. **Implement flows**:37 - Login: validate credentials → issue tokens/cookie → redirect.38 - Register: validate, hash password (bcrypt/argon2), create user.39 - Password reset: time-limited token via email, one-time use.40 - Refresh: short-lived access token + rotating refresh token.41 - Logout: clear cookies, invalidate refresh token on server.42434. **CSRF protection**: Use SameSite + double-submit cookie or framework built-in.44455. **Protected routes / middleware**:46 - Next.js middleware example that checks for valid session/JWT.47 - Redirect unauthenticated users to /login with return URL.48496. **NextAuth.js / Auth.js integration** (recommended for most Next.js apps):50 - Full config with credentials provider + Google/GitHub.51 - Custom pages.52 - Session callback to enrich user object.53547. **Output**:55 - Complete auth service functions.56 - Route handlers or API routes for each flow.57 - Middleware.58 - Frontend login/register forms (or point to form-validator skill).59 - Environment variable list.60 - Security checklist.6162## Examples6364Full NextAuth config, custom JWT login/register handlers with httpOnly cookies, password reset flow with email token, and protected route middleware are included.6566## Edge Cases & Error Handling6768- **Invalid/expired refresh token**: Force re-login, clear all cookies.69- **Brute force**: Rate limit login attempts per email/IP.70- **Email verification**: Optional but recommended step after registration.71- **Account linking**: For OAuth + credentials.72- **Session fixation**: Regenerate session on login.7374## Verification75761. Register a new user.772. Login — verify httpOnly cookie is set (inspect in DevTools → Application → Cookies).783. Access a protected page while logged out — redirected to login.794. Refresh the page while logged in — session persists.805. Logout — cookie cleared.816. Attempt password reset end-to-end.827. Success: All flows work, no tokens in localStorage, cookies are httpOnly and secure-flagged in production.8384## References8586- [Auth.js / NextAuth.js Docs](https://authjs.dev/)87- [OWASP Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)88- [RFC 6749 OAuth 2.0 + PKCE (RFC 7636)](https://datatracker.ietf.org/doc/html/rfc7636)89- [HTTP Cookie Security](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies)