This project demonstrates a simple authentication system using Node.js, Express, PostgreSQL, and JWT. It contains routes for user registration and login, and it utilizes Docker for easy setup and deployment.
- Docker
- Docker Compose
- cURL or Postman for API testing
-
Clone the Repository:
git clone [YOUR_REPOSITORY_LINK] cd node_jwt_postgres
-
Start the Docker Containers:
docker-compose up
This command will build and start the Node.js app and PostgreSQL database containers. The Node.js app will be accessible at http://localhost:3000.
-
Stop the Docker Containers:
When you're done, you can press Ctrl + C in the terminal and then run:
docker-compose down
Endpoint: POST http://localhost:3000/register
Payload:
{
"username": "your_username",
"password": "your_password"
}
cURL Command:
curl -X POST -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpassword"}' http://localhost:3000/register
Endpoint: POST http://localhost:3000/login
Payload:
{
"username": "your_username",
"password": "your_password"
}
cURL Command:
curl -X POST -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpassword"}' http://localhost:3000/login
On successful login, you will receive a JWT token in the response. This token can be used to access protected routes by including it in the Authorization header:
To access a protected route, include the JWT token obtained from the login process in the Authorization header of your request.
Authorization: Bearer YOUR_JWT_TOKEN
Endpoint: GET http://localhost:3000/users
cURL Command:
curl -X GET -H "Authorization: Bearer YOUR_JWT_TOKEN" http://localhost:3000/users
If you want to check the data in the PostgreSQL database:
-
Connect to the PostgreSQL container:
docker exec -it node_jwt_postgres_db_1 psql -U myuser -d mydb
This command might vary based on the name of your PostgreSQL container (node_jwt_postgres_db_1 is just a common naming convention).
-
List the tables:
\dt
-
Query the users table:
SELECT * FROM users;
You should see the user you just registered.
-
Exit the PostgreSQL prompt:
\q
These steps will allow you to test the registration and login functionality as well as inspect the data in your PostgreSQL database.