Create the auth.js file in the utils directory to handle user authentication and authorization using JSON Web Tokens (JWT). This file will provide functions for signing JWT tokens when a user logs in and verifying tokens for authenticated routes. It will also include middleware to attach user information to incoming requests.
Tasks:
Import Required Modules:
Import the jsonwebtoken package to handle JWT creation and verification.
Set Up Configuration:
Define a secret key (secret) and token expiration time (expiration) to be used for signing and verifying JWT tokens.
Create signToken Function:
Implement the signToken function that takes user information (username, email, and _id) and returns a signed JWT.
The function should use the jwt.sign() method to create a token, with the user information as the payload.
Create authMiddleware Function:
Implement the authMiddleware function to authenticate incoming requests.
This function should extract the JWT token from the request body, query parameters, or headers.
If a token is found, it should verify the token using jwt.verify() and attach the decoded user data to the req object.
If no token is found or the token is invalid, the request should proceed without user authentication.
Export the Module:
Export the signToken and authMiddleware functions for use in other parts of the application.
Test Authentication Functionality:
Verify that the signToken function correctly signs tokens and that the authMiddleware function properly authenticates requests and attaches user data to req.
Create the auth.js file in the utils directory to handle user authentication and authorization using JSON Web Tokens (JWT). This file will provide functions for signing JWT tokens when a user logs in and verifying tokens for authenticated routes. It will also include middleware to attach user information to incoming requests.
Tasks:
Import Required Modules:
Import the jsonwebtoken package to handle JWT creation and verification. Set Up Configuration:
Define a secret key (secret) and token expiration time (expiration) to be used for signing and verifying JWT tokens. Create signToken Function:
Implement the signToken function that takes user information (username, email, and _id) and returns a signed JWT. The function should use the jwt.sign() method to create a token, with the user information as the payload. Create authMiddleware Function:
Implement the authMiddleware function to authenticate incoming requests. This function should extract the JWT token from the request body, query parameters, or headers. If a token is found, it should verify the token using jwt.verify() and attach the decoded user data to the req object. If no token is found or the token is invalid, the request should proceed without user authentication. Export the Module:
Export the signToken and authMiddleware functions for use in other parts of the application. Test Authentication Functionality:
Verify that the signToken function correctly signs tokens and that the authMiddleware function properly authenticates requests and attaches user data to req.