agentleFS
kienbui1995kienbui1995/magic-powers/skills/authentication-patterns/SKILL.md

authentication-patterns

Skill0 starsChanged 8 months agoMITView on GitHub

Use when implementing auth - OAuth 2.0, JWT, session management, API keys, RBAC, or reviewing auth security

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Authentication Patterns

Overview

Auth is the most security-critical part of any application. Use proven patterns, never roll your own crypto.

When to Use

  • Implementing login/signup flows
  • Adding OAuth/social login
  • Designing API authentication
  • Reviewing auth security
  • Implementing role-based access control

Pattern Selection

PatternBest ForAvoid When
Session + cookieServer-rendered web appsMobile/SPA without same-origin
JWT (access + refresh)SPAs, mobile apps, microservicesSimple server-rendered apps
OAuth 2.0 + OIDCSocial login, SSO, third-party authInternal-only tools
API keysServer-to-server, public APIsUser-facing auth

JWT Best Practices

  • Short-lived access tokens — 15 minutes max
  • Long-lived refresh tokens — stored in httpOnly cookie, rotated on use
  • Never store JWTs in localStorage — XSS vulnerable
  • Include minimal claims — user ID, roles. Not PII.
  • Validate on every request — check signature, expiry, issuer
  • Use RS256 or ES256 — not HS256 for distributed systems

OAuth 2.0 Flow (Authorization Code + PKCE)

1. Client generates code_verifier + code_challenge
2. Redirect to /authorize?response_type=code&code_challenge=...
3. User authenticates with provider
4. Provider redirects back with authorization code
5. Client exchanges code + code_verifier for tokens
6. Store access token in memory, refresh token in httpOnly cookie

Security Checklist

  • Passwords hashed with bcrypt/argon2 (cost factor ≥12)
  • Rate limiting on login endpoints
  • Account lockout after N failed attempts
  • CSRF protection on session-based auth
  • Refresh token rotation (one-time use)
  • Secure cookie flags: httpOnly, secure, sameSite=strict
  • No secrets in JWTs or URLs

Integration

  • magic-powers:api-design — auth headers and error responses
  • magic-powers:security-review — audit auth implementation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.