A complete and easy to integrate User-Authentication REST API for any application with Express.js, MongoDB as backend and DB respectively
1. Clone or download the repository
2. Open project in your favourite IDE ( I assume VS Code ;) )
3. Run npm install in cloned directory
4. Start MongoDB service in your machine
5. Change the connection string of DB in db/db.js
5. Start the app by running npm run dev and open POSTMAN.
6. Start with POST /register and rest is all yours ;)
1. JWT authentication
2. Simple Mongoose model with one instance method for JWT token generation.
3. Form/JSON data validation using @hapi/joi.
4. Authentication middleware is added for protected routes.
5. Easy to integrate with any Front-end application.
6. Neat and Well documented code for easy understanding.
-
POST Routes
-
/register
Accepts{username,email,password,password_confirm}
and returns user ID on successful registration. -
/login
Accepts{username,password}
and returns JWT "Auth-Token" which can be used for subsequent requests to protected routes -
/user/:id
Returns a user with given{id}
as a URL parameter
-
-
GET routes
-
/users
List all stored users from DB -
/test
Sample protected route -
/logout
Logs user out from current session -
/logoutall
Logs user out from all logged in sessions -
/check
Check if given JWT token is valid or not
-
-
PUT routes
- /update/:id
Update password (or any other details you want) of the user with given id
- /update/:id