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.
Make sure you have Docker installed on your system before getting started.
Make sure you have Docker installed on your system before getting started.
git clone git@github.com:felipeflfranca/authentication-and-authorization-using-JWT-Nest.js.git
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
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.
The application provides endpoints for authentication, user creation with roles, update, delete, and listing. Below are some examples of API requests:
POST /login
Content-Type: application/json
{
"email": "admin@gmail.com",
"password": "teste"
}
POST /refresh
Content-Type: application/json
{
"refresh": "[REFRESH TOKEN]"
}
GET /logout
Content-Type: application/json
Authorization: Bearer [TOKEN]
POST /user
Content-Type: application/json
Authorization: Bearer [TOKEN]
{
"name": "User teste",
"email": "admin@test.com.br"
"password": "test"
"roles": ["admin"]
}
PUT /user/{id}
Content-Type: application/json
Authorization: Bearer [TOKEN]
{
"name": "User teste",
"email": "admin@test.com.br"
"password": "test2"
"roles": ["admin"]
}
DELETE /user/{id}
Authorization: Bearer [TOKEN]
GET /user/all
Authorization: Bearer [TOKEN]
GET /user/{id}
Authorization: Bearer [TOKEN]
@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!