Skip to content

This project is a NestJS application that includes functionality for user authentication and token management. It leverages Prisma as the database client for interaction with the database.

Notifications You must be signed in to change notification settings

felipeflfranca/AuthNestJS

Repository files navigation

Nest TypeScript Starter Project

This repository serves as a starting point for NestJS projects using TypeScript. Below are instructions for setting up the project, running Docker with a PostgreSQL database, and using seed commands to populate the database. Additionally, we provide examples of API requests for easy testing of implemented functionalities.


Environment Setup

Make sure you have Docker installed on your system before getting started.

Clone the repository to your local machine:

Make sure you have Docker installed on your system before getting started.

1. Clone the repository to your local machine

  git clone git@github.com:felipeflfranca/authentication-and-authorization-using-JWT-Nest.js.git


Running

1. Run the following command:

  docker-compose up -d

Now you have the development environment running with all routes responding

We implemented refresh token for jwt renewal and a black list with a trigger that deletes tokens that have already expired


Creating and Running Seeds

We use seed commands to populate the database. To create a new seed, run the following command:

  npm run prisma:create-seed SeedName

For example:

  npm run prisma:create-seed CreateUserSeed

This will create a file named seed_timestamp_SeedName.ts in the seeds directory.

To execute all available seeds, use the following command:

  npm run prisma:seed

The provided examples generate a simple seed, but you can customize the seed logic as needed.


Examples of API Requests

The application provides endpoints for authentication, user creation with roles, update, delete, and listing. Below are some examples of API requests:

Authentication

POST Request for Login:

POST /login
Content-Type: application/json

{
  "email": "admin@gmail.com",
  "password": "teste"
}

POST Request for Refresh token:

POST /refresh
Content-Type: application/json

{
    "refresh": "[REFRESH TOKEN]"
}

GET Request for Logout:

GET /logout
Content-Type: application/json
Authorization: Bearer [TOKEN]


Users

POST Request to Create User:

POST /user
Content-Type: application/json
Authorization: Bearer [TOKEN]

{
  "name": "User teste",
  "email": "admin@test.com.br"
  "password": "test"
  "roles": ["admin"]
}

PUT Request to Update User:

PUT /user/{id}
Content-Type: application/json
Authorization: Bearer [TOKEN]

{
  "name": "User teste",
  "email": "admin@test.com.br"
  "password": "test2"
  "roles": ["admin"]
}

DELETE Request to Delete User:

DELETE /user/{id}
Authorization: Bearer [TOKEN]

GET Request to Get All Users:

GET /user/all
Authorization: Bearer [TOKEN]

GET Request to Get User by ID:

GET /user/{id}
Authorization: Bearer [TOKEN]


Example of using annotation with access permission

@Get('me')
@Roles(Role.Admin)
getMe(@CurrentUser() user: User): User {
  return user;
}



I hope these instructions help with setting up and testing your project. If you encounter issues or have suggestions, please feel free to open an issue or contribute to development. Happy coding!

About

This project is a NestJS application that includes functionality for user authentication and token management. It leverages Prisma as the database client for interaction with the database.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published