This project is a skeleton for a generic NestJS API with an user table, authentication and role based authorization, and was created to be used as a startup for backend projects.
- User creation
- Authentication (with username/password)
- User listing with role based authorization
There are some decorators that can be used on new endpoints, like UseGuards for endpoints that needs authentication, and the Roles that can be used in conjunction with UseGuards to implement role based authorization
Authentication: @nestjs/jwt, @nestjs/passport, passport, passport-jwt, passport-local
Encryption: bcrypt
ORM: Prisma
Utilities: class-transformers, class-validator
After cloning the repo, enter his folder using:
cd base-nestjs-api
Install the dependencies using
npm install
yarn install
Setup the .env file at the root folder. It should be something similar to this:
DATABASE_URL="postgresql://postgres:root@localhost:5432/database?schema=public"
SALT_ROUNDS=10
JWT_SECRET=12345678910
JWT_EXPIRE=3600s
Apply the migration to the database by running the migrate:deploy script:
npm run migrate:deploy
yarn migrate:deploy
Now you can run the application by executing:
npm run dev
yarn dev
And the application will be running on your 3000 port.
POST /users
Parameter | Type | Description |
---|---|---|
email |
string |
Mandatory. The user's email used to sign in |
password |
string |
Mandatory. The user's password |
firstName |
string |
Mandatory. The user's first name |
lastName |
string |
Mandatory. The user's last name |
POST /authenticate/password
Parameter | Type | Description |
---|---|---|
email |
string |
Mandatory. The user's email |
password |
string |
Mandatory. The user's password |
GET /users