Introduction
Authentication middleware sits between incoming requests and your route handlers, verifying that users are who they claim to be before granting access to protected resources. In this lesson, you'll build JWT-based authentication middleware and apply it to specific routes, transforming your open API into a secure, production-ready system.
Middleware functions have access to the request object, response object, and the next middleware function in the stack. Authentication middleware inspects the Authorization header, validates the token, attaches user information to the request, and either passes control to the next handler or rejects the request.
Building the Authentication Middleware
Create a middleware function that extracts and verifies JWT tokens. The middleware checks for a Bearer token in the Authorization header, validates it using your secret key, and attaches the decoded payload to the request object for downstream handlers to use.
The middleware extracts the token by removing the Bearer prefix, verifies it against your secret, and stores the decoded user data in req.user. If verification fails, it returns a 401 status without calling next, stopping the request pipeline.
Protecting Routes with Middleware
Apply the authentication middleware to routes that require user identity. You can protect individual routes, groups of routes, or entire routers by inserting the middleware before your handler functions.
The authenticate middleware runs before the route handler. Routes without the middleware remain publicly accessible. The req.user object populated by the middleware gives handlers access to the authenticated user's identity.
Role-Based Access Control
Extend your middleware to handle authorization by checking user roles or permissions. Create a wrapper function that accepts required roles and returns middleware enforcing those requirements.
The authorize middleware assumes authenticate has already run and attached req.user. It checks the user's role against allowed values and returns 403 Forbidden if the role doesn't match. Chain multiple middleware functions to enforce both authentication and authorization.
Error Handling and Token Refresh
Handle common authentication scenarios gracefully. Distinguish between missing tokens, invalid tokens, and expired tokens to provide clear feedback. Consider implementing refresh tokens for long-lived sessions.
Structured error responses with error codes help clients handle different failure modes appropriately. Expired tokens might trigger a token refresh flow, while invalid tokens require re-authentication.
Summary
Authentication middleware centralizes security logic and applies it consistently across your API. By extracting and verifying JWT tokens, attaching user information to requests, and enforcing role-based access control, you transform open endpoints into a secure system. Always run authentication before authorization, return clear error responses, and consider token expiration and refresh strategies for production deployments.
Lesson Checkpoint
Review what you learned and get feedback on your work.