Skip to content

LucasJunio/authentication-authorization-jwt-inversify-express

Repository files navigation

🧪 Backend Test. 💚

🚧 Finished 🚀 🚧

Prerequisites

Before you need install follow tools in your machine: Git, Node.js, Docker. Beyond is good to has an editor for to work with the code like VSCode

Summary

1. Overview

Welcome to the Authentication and Authorization API! This API allows you to signin making authentication and authorization, receiving a token and a session cookie. This README provides instructions on how to set up and use the API. On this application you can see concepts like SOLID, EventEmitter, DRY, LOGS.

Result:

API

1.1. Stack

1.2. Auxiliary libs

2. 🔬 How To Run

# Clone this repository
$ git clone https://github.com/LucasJunio/authentication-authorization-jwt-inversify-express

# Acces the project folder in your terminal/cmd
$ cd authentication-authorization-jwt-inversify-express

2.1. Containers

# Copy base.env to .env
$ cp base.env .env

# Build the image docker
$ docker build -t auth .

# Build the container docker
$ docker run -p 3001:3001 auth

# The aplication going to open in  port:3001 - access http://localhost:3001

2.2. Local

Run the development server local with "yarn", "npm", "pnpm" or manager package your preference:

# Create .env file
$ cp base.env .env

# Install dependencies
$ yarn install

# Generate the TypeScript code associated with your database models
$ npx prisma generate

# Building migrations
$ npx prisma migrate dev

# Building mock data
$ npx prisma db seed

# Run the project
$ yarn start

The aplication going to open in port:3001 - access http://localhost:3001

2.3 Evaluating Signin and Private Route

You can login by sending a POST request to the /api/v1/login endpoint. Here's an example using cURL:

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{
  {
    "username": "user",
    "password": "123456"
  }
}' \
  http://localhost:3001/api/v1/login

The response will be a token.

You can evaluate the private route by sending a GET request to the /api/v1/private-route endpoint. Here's an example using cURL:

curl -X GET
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN_HERE" \
  -H "Cookie: session=YOUR_COOKIE_HERE" \
 http://localhost:3001/api/v1/private-route

The response will include the machine name and its health score.

2.4 API Endpoints

  • POST /private-route: Checks if isAuthenticatedAndAuthorized middleware is working by getting session token and cookie.
  • POST /login: Generate a token and ssesion.

3. Prisma Local Database

You can see the local database Prisma Studio is up on http://localhost:5555.

# Running local database
$ npx prisma studio

Result:

Prisma Local Database

4. How to create a new branch

Ever that's necessary to create a new branch feature, create by [main]branch, implement your development, merge this feature/branch to develop, when all it's ok, give a merge of this feature/branch to main[branch].

Let's go to follow the git flow patterns to create a new branch, to more info browse in link below to learn more how to implement in simple way.

Workflow gitflow: https://www.atlassian.com/br/git/tutorials/comparing-workflows/gitflow-workflow

# Example creating a new branch
$ git checkout -b feature/TASK-1

4.1. Commits

For commit your changes, you can follow the suggestion to use conventional commits [https://www.conventionalcommits.org/en/v1.0.0/] for to improve your commit descriptions and help the understand of the team. Exist a extension in vscode that can help you with this mission.

vscode extension id in .vscode/extensions.json:

"recommendations": ["vivaxy.vscode-conventional-commits"]

5. Software Architecture

Main folder tree.

C:.

  • src
  • ├───constants
  • ├───core
  • ├───middleware
  • ├───modules
  • │ ├───controllers
  • │ ├───dtos
  • │ ├───inputs
  • │ ├───interfaces
  • │ ├───repositories
  • │ ├───services
  • │ └───validators
  • └───util

6. Recommended Extensions

You can check out a list of recommended extensions in the file .vscode/extensions.json, or by opening the "Extensions" Tab in the "Recommended" pane.


🦸 Author


Lucas Junio
🚀

📝 Licence

Made with ❤️ by Lucas Junio 👋🏽 Contact

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published