Follow these steps to set up and test a simple Express.js JWT authentication API.
Open your terminal and run the following command to install the required dependencies using Yarn:
yarn install
Copy the .env.example file to .env. You can modify the value if needed
Start the development server by running the following command:
yarn dev
Hit the /auth/sign-in endpoint using your preferred tool (e.g., Postman or curl) with an available user from the db.js file. This API call will provide you with a JWT token. Replace <email> and <password> with the actual email and password of the user from the db.js file:
POST http://localhost:3000/auth/sign-in
Body:
{
"email": "<email>",
"password": "<password>"
}This will return a access token and refresh token in the response.
Now, you can test if the access token is working by making an authenticated request to the /me endpoint. Set the JWT token as the Authorization header in the request. Replace <your_token> with the JWT token obtained in Step 3:
GET http://localhost:3000/me
Headers:
Authorization: Bearer <your_token>If the token is valid, you should receive a response with the user's information. If it's invalid, you will get an authentication error.