NodeJS app on express framework backed by a MongoDB database following MVC pattern. Includes User Model with CRUD services, as well signin/signup endpoints. Passwords are encrypted using bcryptjs.
Authentication using Email and Password
Add (signup/signin)
/ Update / Delete Users
backend
├── config
│ └── database.js
├── controllers
│ └── User.controller.js
├── middleware
│ └── auth.js
├── models
│ └── User.js
├── routes
│ └── User.routes.js
├── server.js
└── services
└── UserService.js
- config/ - Mongoose config to connect with MongoDB.
- controllers/ - Serves the responses.
- middleware/ - Verifies JWT Token.
- models/ - Schema definitions for mongoose models.
- routes/ - Routes for API.
- server.js - Entry point of application.
- services/ - Business logic between controllers and models.
https://github.com/luisgcenci/signup-login-nodejs-backend-app.git
yarn install
TOKEN_KEY is used to create JWT Auth.
MONGO_URI="mongodb+srv://username:password@databasename.bmpbw.mongodb.net/?retryWrites=true&w=majority"
TOKEN_KEY="RANDOMSTRING"
node server.js
Method : POST
Auth required : NO
Data Params (Body)
{
"username": "username",
"password": "password"
}
Code : 201 OK
Returns JWT Auth Token (application/json)
Content example
{
"jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0"
}
Method : POST
Auth required : NO
Data Params (Body)
{
"username": "username",
"password": "password"
}
Code : 201 OK
Returns JWT Auth Token (Application JSON)
Content example
{
"jwt_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0"
}
Method : POST
Auth required : NO
Data Params (Body)
{
"username": "username",
"password": "password",
"newUsername": "newUsername"
}
Code : 201 OK
Returns user object (application/json)
Content example
{
"_id": "62cf9be8f3c2a3447e210277",
"username": "newUsername",
"password": "$2a$10$Z9XNnzvPA77VnggiEtE9juJvFNXFbVVMtSrhNJTPDfpKLM2jF/keO",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0",
"__v": 0
}
Method : POST
Auth required : NO
Data Params (Body)
{
"username": "username",
"password": "password",
"newPassword": "newPassword"
}
Code : 201 OK
Returns user object (Application JSON)
Content example
{
"_id": "62cf9be8f3c2a3447e210277",
"username": "username",
"password": "newPasswordHashed",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJjZjliZThmM2MyYTM0NDdlMjEwMjc3IiwidXNlcm5hbWUiOiJyb290IiwiaWF0IjoxNjU3NzczMDMyLCJleHAiOjE2NTc3ODAyMzJ9.2avOPBKgWKcLYdmjs6z5j8Yr8rgi4GossoFyLC6pEg0",
"__v": 0
}
Method : DELETE
Auth required : NO
Data Params (Body)
{
"username": "username",
"password": "password"
}
Code : 201 OK
Returns the count of users deleted. (Application JSON)
Content example
{
"deletedCount": 1
}