Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An API Boilerplate to create a ready-to-use REST API in seconds with NestJS v11.x + TypeORM and JWT Auth 😻

License

Notifications You must be signed in to change notification settings

Tony133/nestjs-api-boilerplate-jwt

Repository files navigation

Nest Logo

NestJSApiBoilerplateJWT

An API Boilerplate to create a ready-to-use REST API in seconds with NestJS 11.x and JWT Auth System 😻

Installation

 pnpm install

Set Environment for secret key JWT and other configurations

 cp .env.example .env

To set up on multiple environments, such as development, staging or production, we do as follows:

 cp .env.example .env.development # or .env.staging, etc

Config settings .env for sending a notification when a user registers, forgets password or changes password

 EMAIL_HOST=smtp.mailtrap.io
 EMAIL_PORT=2525
 EMAIL_AUTH_USER=[:user]
 EMAIL_AUTH_PASSWORD=[:password]
 EMAIL_DEBUG=true
 EMAIL_LOGGER=true

Config settings .env to connect MySQL

Once the database has been configured, start the Nest App via pnpm run start:dev it automatically synchronizes the entities so it is ready to use.

 TYPEORM_CONNECTION = "mysql"
 TYPEORM_HOST = "localhost"
 TYPEORM_PORT = 3306
 TYPEORM_USERNAME = [:user]
 TYPEORM_PASSWORD = [:password]
 TYPEORM_DATABASE = [:database]
 TYPEORM_AUTO_SCHEMA_SYNC = true
 TYPEORM_ENTITIES = "dist/**/*.entity.js"
 TYPEORM_SUBSCRIBERS = "dist/subscriber/**/*.js"
 TYPEORM_MIGRATIONS = "dist/migrations/**/*.js"
 TYPEORM_ENTITIES_DIR = "src/entity"
 TYPEORM_MIGRATIONS_DIR = "src/migration"
 TYPEORM_SUBSCRIBERS_DIR = "src/subscriber"

Install TypeScript Node

 pnpm install -g ts-node

Running migrations with typeorm

 ts-node node_modules/.bin/typeorm migration:run -d dist/typeorm-cli.config

or

 node_modules/.bin/typeorm migration:run -d dist/typeorm-cli.config

Running the app

 # development
 $ pnpm start
 # watch mode
 $ pnpm start:dev
 # production mode
 $ pnpm start:prod

Running the app in REPL mode

 pnpm start --entryFile repl

or

 pnpm start:repl

Docker

There is a docker-compose.yml file for starting MySQL with Docker.

$ docker-compose up db

After running, you can stop the Docker container with

$ docker-compose down

Url Swagger for Api Documentation


http://127.0.0.1:3000/docs

or


http://127.0.0.1:3000/docs-json

or


http://127.0.0.1:3000/docs-yaml

Configure SWAGGER_USER and SWAGGER_PASSWORD in the .env file for to access the Swagger(Open API) documentation with basic authentication. NODE_ENV must not be equal to "production" otherwise the Swagger is not displayed.

NODE_ENV=[:enviroments]
SWAGGER_USER=[:user]
SWAGGER_PASSWORD=[:password]

Configuring the SERVER_PORT environment variable as the default port if you don't want to use the default

 SERVER_PORT=3333

Configuring the ENDPOINT_URL_CORS environment variable for app frontend

 ENDPOINT_URL_CORS='http://127.0.0.1:4200'

Getting secure resource with Curl

 curl -H 'content-type: application/json' -v -X GET http://127.0.0.1:3000/api/secure -H 'Authorization: Bearer [:token]'

Generate Token JWT Authentication with Curl

 curl -H 'content-type: application/json' -v -X POST -d '{"email": "tony_admin@nest.com", "password": "mysecret"}' http://127.0.0.1:3000/api/auth/login

Registration user with Curl

 curl -H 'content-type: application/json' -v -X POST -d '{"name": "tony", "email": "tony_admin@nest.com", "username":"tony_admin", "password": "mysecret"}' http://127.0.0.1:3000/api/auth/register

Refresh token with curl

 curl -H 'content-type: application/json' -v -X POST -d '{"refreshToken": "[:token]"}' http://127.0.0.1:3000/api/auth/refresh-tokens

Forgot password with curl

 curl -H 'content-type: application/json' -v -X POST -d '{"email": "tony_admin@nest.com"}' http://127.0.0.1:3000/api/auth/forgot-password

Change password User with curl

 curl -H 'content-type: application/json' -v -X POST -d '{"email": "tony_admin@nest.com", "password": "new_password"}' http://127.0.0.1:3000/api/auth/change-password -H 'Authorization: Bearer [:token]'

Update profile User with curl

 curl -H 'content-type: application/json' -v -X PUT -d '{"name": "tony", "email": "tony_admin@nest.com", "username": "tony_admin"}' http://127.0.0.1:3000/api/users/:id/profile -H 'Authorization: Bearer [:token]'

Users list with Curl

 curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:3000/api/users -H 'Authorization: Bearer [:token]'

User by Id with Curl

 curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X GET http://127.0.0.1:3000/api/users/:id -H 'Authorization: Bearer [:token]'

Update User with Curl

 curl -H 'content-type: application/json' -v -X PUT -d '{"name": "tony", "email": "tony_admin@nest.com", "username": "tony_admin", "password":"password_update"}' http://127.0.0.1:3000/api/users/:id -H 'Authorization: Bearer [:token]'

Delete User by Id with Curl

 curl -H 'content-type: application/json' -H 'Accept: application/json' -v -X DELETE http://127.0.0.1:3000/api/users/:id -H 'Authorization: Bearer [:token]'

License

MIT licensed

About

An API Boilerplate to create a ready-to-use REST API in seconds with NestJS v11.x + TypeORM and JWT Auth 😻

Topics

Resources

License

Stars

Watchers

Forks

Contributors 5

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /