Full Auth System Guide
Build a production-ready authentication system with QAuth. This tutorial covers signup, login, sessions, protected routes, and token refresh.
In this guide
1. Architecture Overview
A typical QAuth-powered system has three components: browser (with QAuthClient), API server (with QAuthValidator), and auth server (with QAuthServer).
Browser
Creates proofs, stores tokens
API Server
Validates tokens & proofs
Auth Server
Issues & refreshes tokens
Database
Users, sessions, tokens
2. Database Schema
Three tables: users, sessions (linked to token JTI), and refresh tokens with rotation support.
3. Server Setup
Initialize QAuthServer once at startup. Share public keys with API servers.
4. User Signup
Hash password with bcrypt, create user record, issue QAuth token, create session and refresh token.
5. User Login
Verify credentials, then issue a fresh token and refresh token. Same flow as signup minus user creation.
6. Session Middleware
Intercept every API request, validate the QAuth token, and pass user info to route handlers.
Next.js Middleware
Express.js Middleware
7. Protected API Routes
Validate the token (done by middleware), extract user info, then check policy authorization.
8. Token Refresh
Refresh token rotation: the old refresh token is revoked, and a new pair (access + refresh) is issued. This detects token theft — if a revoked token is reused, invalidate all sessions for that user.
9. Logout
Delete the session record (cascades to refresh tokens). The access token remains valid until expiry (max 1 hour), but the session cannot be refreshed.
10. Complete Express.js Server
A minimal but complete Express.js server with QAuth. Copy-paste ready.