Skip to content

NodeJS-Express | Restful API with MongoDB : User Authentication, Authentication JWT (Json Web Token) & Device Detector User Login

Notifications You must be signed in to change notification settings

sabo99/nodejs-restfulapi-jwt-mongob

Repository files navigation

Restful API with MongoDB : User Authentication + CRUD, Authentication JWT (Json Web Token) & Device Detector User Login

Technology Used

  • 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

Project Structure


APIs Provided

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

Flow for SignUp & SignIn with JWT Authentication

Following diagram shows the flow that we will implement for the User Registration, User Login, and Authenticate JWT Processes.



APIs Specification


Base APIs

Request :

  • Method : GET

  • Endpoint : /

  • Header :

    • Content-Type : application/json
    • Accept : application/json

  • Response :

    {
        "code": "number",
        "message": "string"
    }

User Registration

Request :

  • Method : POST

  • Endpoint : /api/auth/signup

  • Header :

    • Content-Type : application/json
    • Accept : application/json

  • Body :

    {
        "email": "string",
        "username": "string",
        "password": "string, hash"
    }
  • Response :

    {
        "code": "number",
        "message": "string",
        "user": {
            "_id": "string",
            "email": "string",
            "username": "string",
            "createdAt": "date-string"
        }
    }

User Login

Request :

  • Method : POST

  • Endpoint : /api/auth/signin

  • Header :

    • Content-Type : application/json
    • Accept : application/json

  • 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"
        }
    }

Public Content

Request :

  • Method : GET

  • Endpoint : /api/user/public

  • Header :

    • Content-Type : application/json
    • Accept : application/json

  • Response :

    {
        "code": "number",
        "message": "string"
    }

User List

Require token

Request :

  • Method : GET

  • Endpoint : /api/user/list

  • Header :

    • Content-Type : application/json
    • Accept : application/json
    • x-auth-token : string

  • Response :

    {
        "code": "number",
        "message": "string",
        "user": [
            {
                "_id": "string",
                "email": "string",
                "username": "string",
                "createdAt": "date-string"
            },
            {
                "_id": "string",
                "email": "string",
                "username": "string",
                "createdAt": "date-string"
            }
        ]
    }

Find User

Require token

example: /api/user/find?id=1

Request :

  • Method : GET

  • Endpoint : /api/user/find

  • Query :

    • id : string
  • Header :

    • Content-Type : application/json
    • Accept : application/json
    • x-auth-token : string

  • Response :

    {
        "code": "number",
        "message": "string",
        "user": {
            "_id": "string",
            "email": "string",
            "username": "string",
            "createdAt": "date-string"
        }
    }

Update User

Require token

Request :

  • Method : PUT / PATCH

  • Endpoint : /api/user/{id}/update

  • Header :

    • Content-Type : application/json
    • Accept : application/json
    • x-auth-token : string

  • Body :

    {
        "email": "string",
        "username": "string",
        "password": "string, hash"
    }
  • Response :

    {
        "code": "number",
        "message": "string"
    }

Delete User

Require token

Request :

  • Method : DELETE

  • Endpoint : /api/user/{id}/delete

  • Header :

    • Content-Type : application/json
    • Accept : application/json
    • x-auth-token : string

  • Response :

    {
        "code": "number",
        "message": "string"
    }

About

NodeJS-Express | Restful API with MongoDB : User Authentication, Authentication JWT (Json Web Token) & Device Detector User Login

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published