Skip to content

chlasswg26/tokopaedi-backend

Repository files navigation

project-image

Simplified backend for e-commerce apps

Demo


FEATURES

  • ROBUST ROUTES (Validation & Sanitizer)
  • AUTHENTICATION & AUTHORIZATION
  • JWT (Token & Refresh Token)
  • HASH (Password with Argon2 Winner of PHC)
  • ENCRYPTION (Some data)
  • COOKIE (HTTP Only Cookie)
  • HANDLER (Error & Form Data)
  • STANDARIZE (Error & Code)
  • CACHE (Redis)
  • LINTER (Code)
  • MANY MORE....

TECH USED

JavaScript Express.js JWT NPM NodeJS Yarn Postgres Redis ESLint Postman

  • JavaScript
  • NodeJS
  • ExpressJS
  • PostgreSQL
  • Redis
  • Cloudinary
  • Argon2

DETAILS

Dependencies

  • argon2: An Argon2 library for Node
  • cloudinary: Cloudinary NPM for node.js integration
  • cookie-parser: Parse HTTP request cookies
  • cors: Node.js CORS middleware
  • cross-env: Run scripts that set and use environment variables across platforms
  • dotenv: Loads environment variables from .env file
  • duration-js: small simple library for dealing with durations
  • express: Fast, unopinionated, minimalist web framework
  • express-validator: Express middleware for the validator module.
  • global: Require global variables
  • helmet: help secure Express/Connect apps with various HTTP headers
  • http-errors: Create HTTP error objects
  • jsonwebtoken: JSON Web Token implementation (symmetric and asymmetric)
  • morgan: HTTP request logger middleware for node.js
  • multer: Middleware for handling multipart/form-data.
  • nodemailer: Easy as cake e-mail sending from your Node.js applications
  • nodemon: Simple monitor script for use during development of a Node.js app.
  • pg: PostgreSQL client - pure javascript & libpq with the same API
  • redis: A modern, high performance Redis client
  • serve-favicon: favicon serving middleware with caching
  • string-crypto: Small and simple (yet secure) library to encrypt and decrypt strings using PBKDF2 for key derivation and AES (defaulted to 256-bit / SHA512)
  • xss-clean: middleware to sanitize user input

Dev Dependencies

Environment

Environment Value Description
PORT 5000 Port
PGHOST localhost Database host
PGPORT 8080 Database port
PGDATABASE postgres Database name
PGUSER postgres Database username
PGPASSWORD - Database password
FRONTEND_URL your_frontend_url Frontend url without slash in the end for Cross Origin (CORS)
CLOUDINARY_URL cloudinary:// Cloudinary URL
SMTP_HOST - SMTP host
SMTP_PORT - SMTP port
SMTP_USERNAME - SMTP username
SMTP_PASSWORD - SMTP password
REDIS_URL rediss://default:password@host:port Redis url cluster for production
REDIS_CACHE_LIFE 3m Redis cache expiration (3 minutes or more)
JWT_SECRET_KEY - JWT Secret Key
JWT_REFRESH_SECRET_KEY - JWT Secret Key (Refresh token)
JWT_TOKEN_LIFE 4h JWT Life (4 hours or more)
JWT_REFRESH_TOKEN_LIFE 1d JWT Life (Refresh token 1 day or more)
JWT_ALGORITHM HS256 JWT Algorithm (see on wikipedia algorithm programming)
ENCRYPTION_PASSWORD - Encryption password (your password)
ENCRYPTION_SALT - Encryption salt (your salt)
ENCRYPTION_DIGEST sha512 Encryption digest (see on wikipedia algorithm digest)
SITE_NAME site_name Site name
MAX_FILE_SIZE 5 File size number (5mb or more)
COOKIE_SECRET_KEY - Cookie secret key (random)
EMAIL_SERVICE support@example.com Email service's (Customer Care)

API Reference

Endpoint Development Endpoint Production
https://localhost:8080/api/v1 https://tokopaedi.up.railway.app/api/v1

AUTHENTICATION

Post registration

  POST /api/v1/auth/register
Parameter Type Description
name string Required. Name of new user to insert
email string Required. Email of new user to verification
password string Required. Password of new user to sign in
picture file Optional. Picture of new user
role string Required. Role of new user to authorization

Get verification account by code

  GET /api/v1/auth/verification/:code
Parameter Type Description
code string Required. Long text of code to verification

Post login

  POST /api/v1/auth/login
Parameter Type Description
email string Required. Email of existing user to sign in
password string Required. Password of existing user to sign in

Get refresh token of logged user

  GET /api/v1/auth/refresh-token
Parameter Type Description
- - -

Get logout of logged user

  GET /api/v1/auth/logout

INCLUDE BEARER TOKEN!

Parameter Type Description
- - -

USERS

Get all users

  GET /api/v1/users

INCLUDE BEARER TOKEN!

Parameter Type Description
search string Optional. Keyword to search users
page integer Optional. Current page of users
limit integer Optional. Limit data of users to show
orderBy string Optional. Ordering data by key name
sortBy string Optional. Sorting data by ASCENDING or DESCENDING (ASC / DESC)

Get user by id

  GET /api/v1/users/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of user to fetch

Post new user data

  POST /api/v1/users

INCLUDE BEARER TOKEN!

Parameter Type Description
name string Required. Name of new user to insert
email string Required. Email of new user to insert
password string Required. Password of new user to insert
picture file Optional. Picture of new user
role string Required. Role of new user to insert

Put existing user data

  PUT /api/v1/users/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of user to update
name string Optional. New name of existing user to update
email string Optional. New email of existing user to update
password string Optional. New password of existing user to update
picture file Optional. New picture of existing user to update
role string Optional. New role of existing user to update

Delete existing user data

  DELETE /api/v1/users/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of user to delete

CATEGORIES

Get all categories

  GET /api/v1/categories
Parameter Type Description
search string Optional. Keyword to search categories
page integer Optional. Current page of categories
limit integer Optional. Limit data of categories to show
orderBy string Optional. Ordering data by key name
sortBy string Optional. Sorting data by ASCENDING or DESCENDING (ASC / DESC)

Get category by id

  GET /api/v1/categories/:id
Parameter Type Description
id integer Required. Id of category to fetch

Post new category data

  POST /api/v1/categories

INCLUDE BEARER TOKEN!

Parameter Type Description
name string Required. Name of new category to insert

Put existing category data

  PUT /api/v1/categories/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of category to update
name string Required. New name of existing category to update

Delete existing category data

  DELETE /api/v1/categories/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of category to delete

PRODUCTS

Get all products

  GET /api/v1/products
Parameter Type Description
search string Optional. Keyword to search products
page integer Optional. Current page of products
limit integer Optional. Limit data of products to show
orderBy string Optional. Ordering data by key name
sortBy string Optional. Sorting data by ASCENDING or DESCENDING (ASC / DESC)

Get product by id

  GET /api/v1/products/:id
Parameter Type Description
id integer Required. Id of product to fetch

Post new product data

  POST /api/v1/products

INCLUDE BEARER TOKEN!

Parameter Type Description
title string Required. Title of new product to insert
description string Required. Description of new product to insert
price integer Required. Price of new product to insert
thumbnail file Optional. Thumbnail of new product
seller_id integer Required. Seller ID of new product to constraint
category_id integer Required. Category ID of new product to constraint

Put existing product data

  PUT /api/v1/products/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of product to update
title string Optional. New title of existing product to update
description string Optional. New description of existing product to update
price integer Optional. New price of existing product to update
thumbnail file Optional. New thumbnail of existing product to update
seller_id integer Optional. New seller ID of existing product to constraint
category_id integer Optional. New sategory ID of existing product to constraint

Delete existing product data

  DELETE /api/v1/products/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of product to delete

TRANSACTIONS

Get all transactions

  GET /api/v1/transactions

INCLUDE BEARER TOKEN!

Parameter Type Description
search string Optional. Keyword to search transactions
page integer Optional. Current page of transactions
limit integer Optional. Limit data of transactions to show
orderBy string Optional. Ordering data by key name
sortBy string Optional. Sorting data by ASCENDING or DESCENDING (ASC / DESC)

Get transaction by id

  GET /api/v1/transactions/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of transaction to fetch

Post new transaction data

  POST /api/v1/transactions

INCLUDE BEARER TOKEN!

Parameter Type Description
buyer_id integer Required. Buyer ID of new transaction to constraint
product_id integer Required. Product ID of new transaction to constraint
quantity integer Required. Quantity of new transaction to insert
price integer Required. Price of new transaction to insert
status string Optional. Status of new transaction to insert (pending, success, failed)

Put existing transaction data

  PUT /api/v1/transactions/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of transaction to update
buyer_id integer Optional. New buyer ID of existing transaction to constraint
product_id integer Optional. New product ID of existing transaction to constraint
quantity integer Optional. New quantity of existing transaction to update
price integer Optional. New price of existing transaction to update
status string Optional. New status of existing transaction to update (pending, success, failed)

Delete existing transaction data

  DELETE /api/v1/transactions/:id

INCLUDE BEARER TOKEN!

Parameter Type Description
id integer Required. Id of transaction to delete

Installation

This is a Node.js module available through the npm registry. It can be installed using the npm or yarn command line tools.

Development

Clone the project

  git clone https://github.com/chlasswg26/tokopaedi-backend

Go to the project directory

  cd tokopaedi-backend

Rename environment files .env.example to .env and filled up the environment variables

mv .env.example .env

Install dependencies

  yarn install

Start the server

  yarn dev

Production

Deploy on Railway


Acknowledgements


Author

👤 Ichlas Wardy Gustama ichlaswardy26@gmail.com


Show your support

Give a ⭐️ if this project helped you!


📝 License

Copyright © 2022 Ichlas Wardy Gustama ichlaswardy26@gmail.com.

This project is MIT licensed.