A Basic Authentication System with user registration and login functionalities using Node.js , Express and Mysql . This project uses JWT for authenticating the users using tokens
- It consists of following RESTful endpoints :
- POST /register: Register a new user.
- POST /login: Login a user and return a JWT.
- GET /profile: Retrieve the logged-in user's profile information (protected endpoint).
- Backend: Express.js , MYSQL , JWT , RESTAPI
- Testing: Jest,supertest
- Node.js and npm installed
- Install MYSQL or MYSQL workbench and create a user
-
clone the repository
-
Install all dependencies.
-
Create a .env file and enter the following details :
- jwt_secret_key
- user
- host
- password
- Database name
-
Run the following SQL commands to set up the database and users table.
-
Start the application
npm start
-
Run the following endpoints in postman or any other api requests extensions
-
Register :
POST -H "Content-Type: application/json" -d '{"username":"testuser","password":"testpass"}' http://localhost:3000/api/auth/register
-
Login :
POST -H "Content-Type: application/json" -d '{"username":"testuser","password":"testpass"}' http://localhost:3000/api/auth/login
-
Access protected route:
-H "Authorization": Bearer <your_jwt_token> http://localhost:3000/api/auth/protected
-
Unit tests are written using jest. To run tests :
npm test