This repository contains an example implementation of JSON Web Token (JWT) authentication in Node.js. It demonstrates how to generate and verify JWT tokens using the jsonwebtoken
library in Node.js.
To install the dependencies, run the following command in the project directory:
npm install
Setup the MongoDB URL on .env
file
To start the server, run the following command:
npm start
This will start the server at http://localhost:3000
To register user into the database, send a POST request to the /register
endpoint with the following payload:
{
"username": "testuser",
"password": "testpassword",
"email": "testemail@example.com"
}
The server will register user and the credential can now be used to login and get the access token.
To generate a JWT token, send a POST request to the /login endpoint with the following payload:
{
"username": "testuser",
"password": "testpassword"
}
The server will respond with a JWT token, which can be used to authenticate subsequent requests.
To authenticate a request, include the JWT token in the Authorization header of the request, like so:
Authorization: Basic <token>
The server will verify the JWT token and, if it is valid, allow the request to proceed.
This project is licensed under the MIT License - see the LICENSE file for details.