Auth System Setup
Overview
Designs and implements complete authentication and authorization systems for web applications. Covers OAuth 2.0 provider integration (Google, GitHub, Microsoft), session and token management, role-based access control (RBAC), and permission architectures. Generates production-ready code, database migrations, and tests.
Instructions
1. Gather Requirements
Before generating any code, determine:
- Tech stack: Backend framework, database, frontend framework
- Auth method: OAuth providers, email/password, magic links, or combination
- Roles needed: What roles exist? What can each role do?
- Token strategy: Stateless JWT, server-side sessions, or hybrid
- Compliance: GDPR, SOC 2, HIPAA — affects data storage and logging
2. Design the Auth Architecture
Create the auth flow diagram and data model:
- User table: id, email, name, avatar, provider, provider_id, created_at
- Role table: id, name, description
- Permission table: id, resource, action (read/write/delete)
- Role-Permission mapping: role_id, permission_id
- User-Role mapping: user_id, role_id
- Refresh token table: id, user_id, token_hash, family_id, expires_at, revoked_at
3. Implement OAuth Flow
For each OAuth provider:
- Create provider configuration (client ID, secret, scopes, callback URL)
- Implement the authorization redirect with state parameter and PKCE
- Handle the callback: exchange code for tokens, extract user profile
- Provision or update the user record
- Issue application tokens (access + refresh)
Always use PKCE for public clients. Always validate the state parameter.
4. Implement RBAC
Generate the permission-checking middleware:
authorize(resource, action) → middleware function
1. Extract user from request (via JWT or session)
2. Load user roles and permissions (cache with TTL)
3. Check if any role grants the required permission
4. Return 403 with clear error if denied
For row-level security, add ownership filters:
filterByOwnership(resource) → middleware function
1. If user role has wildcard access, skip filter
2. Otherwise, add WHERE clause: resource.owner_id = user.id
3. Apply to SELECT, UPDATE, DELETE queries
5. Generate Tests
Create tests for:
- OAuth flow: successful login, invalid state, expired code
- Token lifecycle: issue, refresh, rotate, revoke
- RBAC: each role accessing allowed and denied resources
- Edge cases: expired tokens, revoked refresh tokens, role changes mid-session
Examples
Example 1: Express + PostgreSQL + Google OAuth
Prompt: "Set up Google OAuth with JWT tokens for my Express app. I need admin and user roles."
Output:
auth/providers/google.ts — OAuth 2.0 + PKCE flow
auth/middleware/authenticate.ts — JWT verification
auth/middleware/authorize.ts — Role checker
migrations/001_auth_tables.sql — Users, roles, permissions, refresh_tokens
auth/services/token.service.ts — JWT issuance with refresh rotation
auth/routes.ts — /auth/google, /auth/callback, /auth/refresh, /auth/logout
tests/auth.test.ts — 18 integration tests
Example 2: Django + GitHub OAuth + Multi-tenant RBAC
Prompt: "Add GitHub login to my Django app. Each organization has its own roles: owner, editor, viewer."
Output:
accounts/providers/github.py — OAuth integration via django-allauth
accounts/models.py — Organization, Membership, Role models
accounts/permissions.py — Per-organization permission backend
accounts/middleware.py — Org context middleware (from subdomain or header)
accounts/decorators.py — @require_org_role('editor') decorator
migrations/0001_multi_tenant_auth.py — Schema migration
tests/test_permissions.py — 22 test cases across org boundaries
Guidelines
- Never store plain-text passwords — use bcrypt with cost factor 12+ or argon2id
- Always use PKCE for OAuth flows, even with confidential clients
- Rotate refresh tokens on every use — detect reuse to identify token theft
- Set short access token TTL — 15 minutes is the standard
- Cache permissions — reload on role change, not on every request
- Log auth events — login, logout, failed attempts, role changes (for audit trail)
- Rate limit auth endpoints — prevent brute force on login and token refresh
- Use httpOnly, Secure, SameSite=Strict cookies for refresh tokens in browsers
- Never put sensitive data in JWT payload — it's base64, not encrypted
1---2name: auth-system-setup3description: >-4 When the user wants to set up authentication and authorization for a web application.5 Use when the user mentions "auth," "login," "OAuth," "SSO," "single sign-on,"6 "role-based access," "RBAC," "permissions," "user roles," "access control,"7 "authentication," or "authorization." Covers OAuth 2.0 provider integration,8 session management, and role/permission architecture. For JWT-specific tasks,9 see jwt-handler. For security review, see security-audit.10license: Apache-2.011compatibility: "Any backend framework (Node.js, Python, Go, etc.)"12metadata:13 source: https://skillmd.com/skills/eliferjunior/auth-system-setup14 author: terminal-skills15 version: "1.0.0"16 category: development17 tags: ["authentication", "authorization", "oauth", "rbac", "security"]18---1920# Auth System Setup2122## Overview2324Designs and implements complete authentication and authorization systems for web applications. Covers OAuth 2.0 provider integration (Google, GitHub, Microsoft), session and token management, role-based access control (RBAC), and permission architectures. Generates production-ready code, database migrations, and tests.2526## Instructions2728### 1. Gather Requirements2930Before generating any code, determine:3132- **Tech stack**: Backend framework, database, frontend framework33- **Auth method**: OAuth providers, email/password, magic links, or combination34- **Roles needed**: What roles exist? What can each role do?35- **Token strategy**: Stateless JWT, server-side sessions, or hybrid36- **Compliance**: GDPR, SOC 2, HIPAA — affects data storage and logging3738### 2. Design the Auth Architecture3940Create the auth flow diagram and data model:4142- **User table**: id, email, name, avatar, provider, provider_id, created_at43- **Role table**: id, name, description44- **Permission table**: id, resource, action (read/write/delete)45- **Role-Permission mapping**: role_id, permission_id46- **User-Role mapping**: user_id, role_id47- **Refresh token table**: id, user_id, token_hash, family_id, expires_at, revoked_at4849### 3. Implement OAuth Flow5051For each OAuth provider:52531. Create provider configuration (client ID, secret, scopes, callback URL)542. Implement the authorization redirect with state parameter and PKCE553. Handle the callback: exchange code for tokens, extract user profile564. Provision or update the user record575. Issue application tokens (access + refresh)5859Always use PKCE for public clients. Always validate the state parameter.6061### 4. Implement RBAC6263Generate the permission-checking middleware:6465```66authorize(resource, action) → middleware function67 1. Extract user from request (via JWT or session)68 2. Load user roles and permissions (cache with TTL)69 3. Check if any role grants the required permission70 4. Return 403 with clear error if denied71```7273For row-level security, add ownership filters:7475```76filterByOwnership(resource) → middleware function77 1. If user role has wildcard access, skip filter78 2. Otherwise, add WHERE clause: resource.owner_id = user.id79 3. Apply to SELECT, UPDATE, DELETE queries80```8182### 5. Generate Tests8384Create tests for:8586- OAuth flow: successful login, invalid state, expired code87- Token lifecycle: issue, refresh, rotate, revoke88- RBAC: each role accessing allowed and denied resources89- Edge cases: expired tokens, revoked refresh tokens, role changes mid-session9091## Examples9293### Example 1: Express + PostgreSQL + Google OAuth9495**Prompt**: "Set up Google OAuth with JWT tokens for my Express app. I need admin and user roles."9697**Output**:98- `auth/providers/google.ts` — OAuth 2.0 + PKCE flow99- `auth/middleware/authenticate.ts` — JWT verification100- `auth/middleware/authorize.ts` — Role checker101- `migrations/001_auth_tables.sql` — Users, roles, permissions, refresh_tokens102- `auth/services/token.service.ts` — JWT issuance with refresh rotation103- `auth/routes.ts` — /auth/google, /auth/callback, /auth/refresh, /auth/logout104- `tests/auth.test.ts` — 18 integration tests105106### Example 2: Django + GitHub OAuth + Multi-tenant RBAC107108**Prompt**: "Add GitHub login to my Django app. Each organization has its own roles: owner, editor, viewer."109110**Output**:111- `accounts/providers/github.py` — OAuth integration via django-allauth112- `accounts/models.py` — Organization, Membership, Role models113- `accounts/permissions.py` — Per-organization permission backend114- `accounts/middleware.py` — Org context middleware (from subdomain or header)115- `accounts/decorators.py` — @require_org_role('editor') decorator116- `migrations/0001_multi_tenant_auth.py` — Schema migration117- `tests/test_permissions.py` — 22 test cases across org boundaries118119## Guidelines120121- **Never store plain-text passwords** — use bcrypt with cost factor 12+ or argon2id122- **Always use PKCE** for OAuth flows, even with confidential clients123- **Rotate refresh tokens** on every use — detect reuse to identify token theft124- **Set short access token TTL** — 15 minutes is the standard125- **Cache permissions** — reload on role change, not on every request126- **Log auth events** — login, logout, failed attempts, role changes (for audit trail)127- **Rate limit auth endpoints** — prevent brute force on login and token refresh128- **Use httpOnly, Secure, SameSite=Strict cookies** for refresh tokens in browsers129- **Never put sensitive data in JWT payload** — it's base64, not encrypted