Restful API with MongoDB : User Authentication + CRUD, Authentication JWT (Json Web Token) & Device Detector User Login
bcryptjs: ^2.4.3
device-detector-js: ^3.0.0
cors: ^2.8.5
dotenv: ^10.0.0
express: ^4.17.1
joi: ^17.4.2
jsonwebtoken: ^8.5.1
mongoose: ^6.0.12
nodemon: ^2.0.15
Methods | Urls | Actions | Token |
---|---|---|---|
GET | / | Retrieve Base on APIs | - |
POST | /api/auth/signup | SignUp new Account | - |
POST | /api/auth/signin | Login an Account | - |
GET | /api/user/public | Retrieve Public Content | - |
GET | /api/user/list | Retrieve Users List Content | Required |
GET | /api/user/find | Retrieve User Content | Required |
PUT / PATCH | /api/user/{id}/update | Update User Content | Required |
DELETE | /api/user/{id}/delete | Delete User Content | Required |
Following diagram shows the flow that we will implement for the User Registration
, User Login
, and Authenticate JWT
Processes.
Request :
-
Method :
GET
-
Endpoint :
/
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- Content-Type :
-
Response :
{ "code": "number", "message": "string" }
Request :
-
Method :
POST
-
Endpoint :
/api/auth/signup
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- Content-Type :
-
Body :
{ "email": "string", "username": "string", "password": "string, hash" }
-
Response :
{ "code": "number", "message": "string", "user": { "_id": "string", "email": "string", "username": "string", "createdAt": "date-string" } }
Request :
-
Method :
POST
-
Endpoint :
/api/auth/signin
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- Content-Type :
-
Body :
{ "username": "string", "password": "string, hash" }
-
Response :
{ "code": "number", "message": "string", "user": { "_id": "string", "email": "string", "username": "string", "createdAt": "date-string" }, "device-info": { "client": "object", "os": "object", "device": "object", "bot": "object" } }
Request :
-
Method :
GET
-
Endpoint :
/api/user/public
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- Content-Type :
-
Response :
{ "code": "number", "message": "string" }
Request :
-
Method :
GET
-
Endpoint :
/api/user/list
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- x-auth-token :
string
- Content-Type :
-
Response :
{ "code": "number", "message": "string", "user": [ { "_id": "string", "email": "string", "username": "string", "createdAt": "date-string" }, { "_id": "string", "email": "string", "username": "string", "createdAt": "date-string" } ] }
Request :
-
Method :
GET
-
Endpoint :
/api/user/find
-
Query :
- id :
string
- id :
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- x-auth-token :
string
- Content-Type :
-
Response :
{ "code": "number", "message": "string", "user": { "_id": "string", "email": "string", "username": "string", "createdAt": "date-string" } }
Request :
-
Method :
PUT / PATCH
-
Endpoint :
/api/user/{id}/update
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- x-auth-token :
string
- Content-Type :
-
Body :
{ "email": "string", "username": "string", "password": "string, hash" }
-
Response :
{ "code": "number", "message": "string" }
Request :
-
Method :
DELETE
-
Endpoint :
/api/user/{id}/delete
-
Header :
- Content-Type :
application/json
- Accept :
application/json
- x-auth-token :
string
- Content-Type :
-
Response :
{ "code": "number", "message": "string" }