Here is what you need to get it up and running:
- A running PostgreSQL Server
- An empty DB on the PostgreSQL
nodeandnpminstalled- Postman or cURL (for testing the API)
- A free
8080PORT (you may change the port inserver.js)
- Clone the repo
git clone https://github.com/cstayyab/express-psql-login-api.git
cd express-psql-login-api- Install the dependencies
npm installYou will have to update some values in config/db.config.js
| key | description |
|---|---|
| HOST | your host address on which postgreSQL is installed. Most probably localhost |
| USER | Username of the postgreSQL User (default: postgres) |
| PASSWORD | Password to the PostgreSQL User. You might have to set this via \password username command of PostgreSQL Shell |
| DB | The DB Name of the empty DB you created in the second step of Pre-requisites |
You are now ready to start the API Server. Run the following command from the root of the repository:
npm startHere is a test run with output:
Remember to set
Content-Typetoapplication/jsonfor requests with body
curl --location --request GET 'http://localhost:8080/'{
"message": "Welcome to Login System",
"developer": {
"name": "Muhammad Tayyab Sheikh",
"alias": "cstayyab"
}
}curl --location --request POST 'http://localhost:8080/user/signup' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "cstayyab",
"email": "cstayyab@gmail.com",
"password": "Test@1"
}'{
"message": "Signup Successful!"
}curl --location --request POST 'http://localhost:8080/user/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "cstayyab",
"password": "Test@1"
}'{
"message": "Login Successful",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImNzdGF5eWFiIiwiZW1haWwiOiJjc3RheXlhYkBnbWFpbC5jb20iLCJpYXQiOjE2MTk0MDcwMTl9.AdPWIFeT0W89lUO85e9jyqJRqxV9ac7mJ4sdqmvPWQA"
}This route is for when user is already logged in and want to update their password.
curl --location --request POST 'http://localhost:8080/user/changepassword' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImNzdGF5eWFiIiwiZW1haWwiOiJjc3RheXlhYkBnbWFpbC5jb20iLCJpYXQiOjE2MTk0MDcwMTl9.AdPWIFeT0W89lUO85e9jyqJRqxV9ac7mJ4sdqmvPWQA' \
--data-raw '{
"oldpassword": "Test@1",
"newpassword": "Test@2"
}'{
"message": "Password Updated Successfully!"
}This route is for when user is logged in but want to change some sensitive information such as Email Address so we require re-authentication
curl --location --request POST 'http://localhost:8080/user/verifypassword' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImNzdGF5eWFiIiwiZW1haWwiOiJjc3RheXlhYkBnbWFpbC5jb20iLCJpYXQiOjE2MTk0MDcwMTl9.AdPWIFeT0W89lUO85e9jyqJRqxV9ac7mJ4sdqmvPWQA' \
--data-raw '{
"password": "Test@2"
}'{
"message": "Password Verification Successful!"
}| Tool | Version String |
|---|---|
| NPM | 6.14.12 |
| Node | v14.16.1 |
| PostgreSQL | psql (PostgreSQL) 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1) |