P
AdvancedAll AIs

Implement Secure JWT Authentication

Implements a complete, 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

Requesting justification for each security choice produces architectural decision documentation, crucial for audits. The HttpOnly cookies vs localStorage comparison is a recurring poorly answered question: forcing a justified recommendation avoids vague responses. Security tests are often the first forgotten.

Use Cases

Securing a REST API for a mobile applicationReplacing server sessions with JWTPreparing an auth system for a security audit

Expected Output

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

Learn more

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

View on Prompt Guide

📬 Get new prompts every week

Join our newsletter and never miss a prompt.