Skip to content

Kodexo-Labs/RBAC-Backend

Repository files navigation

RBAC Backend

⛓️ Installation

follow these steps:

Install the dependencies:

yarn install

Set the environment variables:

touch .env
# open .env and modify the environment variables (if needed)

Docker Compose Commands:

docker compose build
docker compose up

📑 Table of Contents

🪶 Features

  • SQL database: PostgreSQL object relational mapping using TypeORM
  • Validation: schema declaration and validation using zod
  • Error handling: centralized error handling mechanism
  • Dependency management: with Yarn
  • Environment variables: using dotenv, config and envalid
  • CORS: Cross-Origin Resource-Sharing enabled using cors
  • Linting: with ESLint and Prettier

🪟 Commands

Running locally:

yarn run dev

Running in production:

yarn start

👽 Environment Variables

The environment variables can be found and modified in the .env file. They come with these default values:

    PORT*
    NODE_ENV*
    POSTGRES_HOST*
    POSTGRES_PORT*
    POSTGRES_USER*
    POSTGRES_PASSWORD*
    POSTGRES_DB*
    JWT_ACCESS_TOKEN_PRIVATE_KEY*
    JWT_REFRESH_TOKEN_PRIVATE_KEY*

🚧 Project Structure

src\
 |--controllers\    # Route controllers (controller layer)
 |--entities\       # TypeORM Entities (data layer)
 |--middlewares\    # Custom express middlewares
 |--migrations\     # PostrgreSQL Database migrations
 |--routes\         # Routes
 |--schemas\        # Schema declaration and validation
 |--services\       # crud services for controller
 |--utils\          # Utility classes, functions and data-source for DB
 |--index.ts        # App entry point

🅿️ API Endpoints

List of available routes:

Auth routes:
POST /api/auth/register - signup
POST /api/auth/login - login
GET /api/auth/refresh - refresh auth tokens
GET /api/auth/logout - logout\

User routes:
GET api/user/me - get user profile\

⁉️ Error Handling

The app has a centralized error handling mechanism.

Controllers should try to catch the errors and forward them to the error handling middleware (by calling next(error)). For convenience, you can also wrap the controller inside the catchAsync utility wrapper, which forwards the error.

The error handling middleware sends an error response, which has the following format:

{
  "status": 404,
  "message": "Not found"
}

🪝 Validation

Request data is validated using zod. The validation schemas are defined in the src/schemas directory and are used in the routes by providing them as parameters to the validate middleware.

☑️ Linting

Linting is done using ESLint and Prettier.

In this app, ESLint is configured to follow the Airbnb JavaScript style guide with some modifications. It also extends eslint-config-prettier to turn off all rules that are unnecessary or might conflict with Prettier.

To modify the ESLint configuration, update the .eslintrc.json file. To modify the Prettier configuration, update the .prettierrc.json file.

To prevent a certain file or directory from being linted, add it to .eslintignore and .prettierignore.

To maintain a consistent coding style across different IDEs, the project contains .editorconfig

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published