This example demonstrates a JWT (JSON Web Token) based authentication application using Express.js. This application creates a JWT during user login and uses this token to control access to protected routes.
- Node.js
- Express.js
- JSON Web Token (JWT)
- bcrypt (for hashing)
- MongoDB (as database)
-
In the project directory, run the following command to install dependencies:
npm install
-
Copy the
.env.example
file to create a new.env
file and add your MongoDB connection URL and a secret key for JWT inside it. -
Start your MongoDB connection:
mongod
-
Start the application by running the following command:
npm start
-
User Signup: You can create a new user by sending a POST request to the
/user
endpoint. You should sendusername
andpassword
fields in JSON format. -
User Login: You can log in as a user by sending a POST request to the
/auth/login
endpoint. Upon successful login, you will receive a JWT token.
You can use the following environment variables:
PORT
: The port number on which the application will run (default: 3000).MONGODB_CONN
: MongoDB connection URL.JWT_SECRET
: Secret key for JWT tokens.
If you have any feedback or would like to contribute to this project, feel free to submit a pull request to the GitHub repository.