Express Middleware with Authentication
Securing API routes with JWT authentication
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
Write an Express middleware in TypeScript to authenticate requests using JWT. The middleware must: check the Authorization Bearer header, decode and validate the token with the secret key, attach the decoded user to req.user, return explicit 401/403 errors, and handle token expiration. Add structured logging with winston.
Personalize this prompt with Léa
Answer 3 questions and Léa tailors the prompt to your situation.
Use Cases
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Pour une meilleure maintenabilité, isolez la validation du token dans une fonction utilitaire dédiée. Utilisez `jsonwebtoken.verify()` avec `process.env.JWT_SECRET` et augmentez le type Express `Request` pour ajouter `user`. Gérez l’expiration via l’option `expiresIn` côté émission.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.