Lunar Phases for Creative Writing · CodeAmber

How to Implement Secure JWT Authentication in Node.js

How to Implement Secure JWT Authentication in Node.js

Establish a robust authentication system using JSON Web Tokens (JWT) featuring refresh token rotation and secure cookie storage to prevent XSS and CSRF attacks.

What You'll Need

Steps

Step 1: Environment Configuration

Store your JWT secret keys and expiration times in a .env file. Use separate secrets for the Access Token (short-lived) and the Refresh Token (long-lived) to ensure that a compromise of one does not automatically compromise the other.

Step 2: User Authentication and Hashing

Create a registration and login endpoint that verifies user credentials. Use bcryptjs to compare the provided password with the hashed version stored in your database before issuing any tokens.

Step 3: Generating the Token Pair

Upon successful login, generate a short-lived Access Token containing the user ID and essential roles. Simultaneously, generate a long-lived Refresh Token that will be used to request new access tokens without requiring the user to re-authenticate.

Send the Refresh Token to the client via an HttpOnly, Secure, and SameSite=Strict cookie. This prevents client-side JavaScript from accessing the token, significantly reducing the risk of theft via Cross-Site Scripting (XSS).

Step 5: Creating the Protected Route Middleware

Develop a middleware function that extracts the Access Token from the Authorization header. Verify the token using your secret key; if the token is expired or invalid, return a 401 Unauthorized response.

Step 6: Building the Token Refresh Logic

Create a dedicated /refresh endpoint that validates the Refresh Token from the secure cookie. If valid, issue a new Access Token and a new Refresh Token, then invalidate the old refresh token in your database.

Step 7: Implementing Refresh Token Rotation

Store the active Refresh Token in your database linked to the user. If a leaked refresh token is used after it has already been rotated, detect the reuse and immediately revoke all active sessions for that user to prevent unauthorized access.

Step 8: Handling Secure Logout

Implement a logout route that clears the Refresh Token cookie on the client side. Simultaneously, delete the token from your database to ensure it cannot be used again if intercepted.

Expert Tips

See also

Original resource: Visit the source site