P
AdvancedAll AIs

Implement secure JWT authentication

Implement a complete and secure JWT system with refresh tokens, revocation, CSRF protection and secure storage, with justification for each security choice.

Paste in your AI

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

Tu es un expert en sécurité web et authentification. Aide-moi à implémenter une authentification JWT complète et sécurisée.

**Stack backend :** [STACK_BACKEND] (ex: Node.js/Express, Python/FastAPI)
**Stack frontend :** [STACK_FRONTEND]
**Base de données :** [BASE_DONNEES]
**Exigences :** [EXIGENCES] (ex: multi-tenant, SSO, 2FA)

Implémente :
1. **Génération du token** : payload, algorithme (RS256 recommandé), durée de vie
2. **Validation** : middleware de vérification avec gestion des erreurs
3. **Refresh tokens** : rotation, stockage sécurisé, révocation
4. **Stockage côté client** : HttpOnly cookies vs localStorage (avec recommandation justifiée)
5. **Révocation** : blacklist ou short-lived tokens, logout
6. **Protection contre les attaques** : CSRF, fixation de session, token theft
7. **Code complet** : handler login, middleware, refresh endpoint
8. **Tests de sécurité** : cas de test pour les vecteurs d'attaque
9. **Checklist de déploiement** : variables d'environnement, rotation des clés

Pour chaque décision, explique le choix de sécurité.

Why this prompt works

The requirement for justification for each security choice produces documentation of architectural decisions, crucial for audits. The comparison between HttpOnly cookies vs localStorage is a recurring question poorly answered: forcing justified recommendations avoids vague responses. Security tests are often the first to be forgotten.

Use Cases

Secure a REST API for a mobile applicationReplace server sessions with JWTPrepare an auth system for a security audit

Expected Output

Complete JWT implementation with middleware, refresh tokens, revocation, endpoint code, security tests, deployment checklist and design choice justifications.

Learn more

Check the full skill on Prompt Guide to master this technique from A to Z.

View on Prompt Guide