requireAuth
The requireAuth middleware protects routes by ensuring only authenticated users can access them. If the JWT token in the request cookies is valid, the decoded JWT payload is attached to request.user and passed to the next route handler. This allows the route handler to access user information and confirms the request is authenticated.
Usage
Apply requireAuth to any route that requires authentication:
router.get("/protected", requireAuth, functionName); // Only accessible if authenticated
Flow
-
Retrieves the JWT token from
request.cookies.token.request.cookiescontains all cookies sent by the client andtokenis the name of that specific cookie which contains the JWT that we need to check and validate if the user is authenticated.
-
If no token is present, responds with
400 Bad Requestand an error message. -
Verifies the token using the server's
JWT_SECRET.JWT_SECRETis an env variable that stores the secret key for signing and verifying JWTs.
-
If the JWT token is valid:
-
decodes the JWT payload and attaches it to
request.user.- This makes the authenticated user's information available to all subsequent middleware and route handlers.
Example:
const { id: userId } = request.user!;
-
-
Calls
next()to proceed to the route handler.next()is an Express function that passes control to the next middleware or route handler in the stack. If authentication succeeds, this allows the request to continue to the intended endpoint.
-
If verification fails, responds with
500 Internal Server Errorand the error.
Example Error Responses
- Missing/Invalid Token:
{ "error": "No token provided" }
Dependencies
- Express: Handles HTTP requests and responses.
- Jsonwebtoken: Used for creating and verifying JWT tokens.
- IAuthenticatedRequest && IAuthPayload: Custom types for authenticated requests.