A modern authentication system with features like secure password handling, email support, file uploads, and middleware-protected routes.
Detailed documentation on how the authentication system works, including middleware usage, token management, and error handling.
A curated list of resources to help you understand the technologies used in this project, such as Node.js, Express, MongoDB, and JWT.
git clone https://github.com/ravirajbhardwaj/authentication.git
cd authenticationnpm installThis project requires setting up environment variables and generating key pairs for authentication.
- Create a
.envfile in the root directory by copying the.env.examplefile:
cp .env.example .env- Create a
secretsdirectory at the root of the project:
mkdir secrets- Inside the
secretsdirectory, create two files:
private.pem: This will store the private key.public.pem: This will store the public key.
-
Generate a public and private key pair. You can use the following command to generate them: ✅ 1. Generate Private Key (private.pem)
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
- -algorithm RSA → We are using RSA algorithm.
- rsa_keygen_bits:2048 → Sets key size to 2048 bits (standard secure size).
- This generates
private.pem. - The private key is used to sign the JWTs, ensuring that only the server can create valid tokens.
- The private key should be kept secret and secure, as it is used to sign the JWTs.
✅ 2. Extract Public Key (public.pem) from Private Key
openssl rsa -pubout -in private.pem -out public.pem
- -pubout → Extracts the public key from the private key.
- -in private.pem → Specifies the input file (private key).
- -out public.pem → Specifies the output file (public key).
- This generates
public.pem. - The public key is derived from the private key, allowing you to share it without compromising security.
- The private key should be kept secret and secure, while the public key can be shared with anyone who needs to verify the JWTs signed with the private key.
- The public key is used to verify the JWTs, ensuring that they were signed by the server and have not been tampered with.
-
Use the following commands to run the project:
Development mode
pnpm run dev
Production mode
pnpm start
📥 Use the Postman collection below to test all the available APIs:
Import the collection into Postman and set the environment variables like server_url, etc.
Built with ❤️ to simplify auth flows and speed up backend development.