Skip to content

NodeJS-Express | Restful API with MySQL : User Authentication & Authentication JWT (Json Web Token)

Notifications You must be signed in to change notification settings

sabo99/nodejs-restfulapi-jwt-sequelize-mysql

Repository files navigation

Restful API with MySQL
User Authentication + CRUD & Authentication JWT (Json Web Token)

🚀 Quick start

  1. Create a NodeJS app.

    Use the NodeJS CLI to create a new app, specifying the minimal starter.

    # create a new NodeJS site using the minimal starter
    npm init --y
  2. Setting up automatically update server

    ...
    "scripts": {
        ...
        "start": "nodemon server.js"
    },
  3. Start developing.

    Navigate into your new app directory and start it up.

    cd [folder_name]
    npm start
  4. Open the code and start customizing!

    Your server is now running at http://localhost:3000!

    Edit /server.js to see your server update in real-time!

Technology Used

  • bcryptjs: ^2.4.3
  • cors: ^2.8.5
  • dotenv: ^10.0.0
  • express: ^4.17.1
  • joi: ^17.4.2
  • jsonwebtoken: ^8.5.1
  • mysql2: ^2.3.3
  • nodemon: ^2.0.15
  • sequelize: ^6.9.0
  • sequelize-cli: ^6.3.0

Project Structure


APIs Provided

Methods Urls Actions Token
POST /api/auth/signup SignUp new Account -
POST /api/auth/signin Login an Account -
GET /api/user/list Retrieve Users List Content Required
GET /api/user/find Retrieve User Content Required
PUT / PATCH /api/user/{id} Update User Content Required
DELETE /api/user/{id} 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

https://www.getpostman.com/collections/1f14b750916ed2fefd28

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

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}

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

  • Header :

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

  • Response :

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

About

NodeJS-Express | Restful API with MySQL : User Authentication & Authentication JWT (Json Web Token)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published