Giter Site home page Giter Site logo

tony133 / nestjs-api-boilerplate-jwt Goto Github PK

View Code? Open in Web Editor NEW
445.0 5.0 65.0 2.59 MB

An API Boilerplate to create a ready-to-use REST API in seconds with NestJS v10.x + TypeORM and JWT Auth ๐Ÿ˜ป

License: MIT License

TypeScript 99.41% JavaScript 0.53% Shell 0.07%
nestjs nest js javascript node boilerplate jwt rest-api api-rest apirest typeorm swagger openapi docker nodejs typescript

nestjs-api-boilerplate-jwt's Introduction

Nest Logo

NestJSApiBoilerplateJWT

An API Boilerplate to create a ready-to-use REST API in seconds with NestJS 10.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 dev, stage or prod, we do as follows:

   $ cp .env.example .env.dev # or .env.stage, 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 and set NODE_ENV to local or dev or staging to access the SWAGGER(Open API) documentation with basic authentication.


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

If you want to add more environments, include them in the SWAGGER_ENVS array in main.ts, see the following:

const SWAGGER_ENVS = ['local', 'dev', 'staging'];

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

   NODE_API_PORT=3333

Configuring the ENDPOINT_CORS environment variable for app frontend

   ENDPOINT_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": "[email protected]", "password": "secret"}' 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": "[email protected]", "username":"tony_admin", "password": "secret"}' 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": "[email protected]"}' 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": "[email protected]", "password": "secret123"}' 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": "[email protected]", "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": "[email protected]", "username": "tony_admin", "password":"secret"}' 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

nestjs-api-boilerplate-jwt's People

Contributors

dependabot[bot] avatar jmcdo29 avatar renovate[bot] avatar thuongtruong1009 avatar tony133 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nestjs-api-boilerplate-jwt's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

docker-compose
docker-compose.yml
  • mysql 8.3
  • mysql 8.3
github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4@0ad4b8fadaa221de15dcec353f45205ec38ea70b
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/nodejs-environment.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • actions/setup-node v4
npm
package.json
  • @fastify/cors ^9.0.1
  • @fastify/static ^7.0.2
  • @nestjs/common ^10.3.7
  • @nestjs/config ^3.2.1
  • @nestjs/core ^10.3.7
  • @nestjs/jwt ^10.2.0
  • @nestjs/mapped-types 2.0.5
  • @nestjs/platform-fastify ^10.3.7
  • @nestjs/swagger ^7.3.1
  • @nestjs/throttler ^5.1.2
  • @nestjs/typeorm ^10.0.2
  • bcrypt ^5.1.1
  • class-transformer ^0.5.1
  • class-validator ^0.14.1
  • dotenv ^16.4.5
  • fastify ^4.26.2
  • mysql2 ^3.9.3
  • nodemailer ^6.9.13
  • reflect-metadata ^0.2.2
  • rimraf ^5.0.5
  • rxjs ^7.8.1
  • typeorm ^0.3.18
  • yup ^1.3.3
  • @commitlint/cli ^19.2.1
  • @commitlint/config-angular ^19.1.0
  • @nestjs/cli ^10.3.2
  • @nestjs/schematics ^10.1.1
  • @nestjs/testing ^10.3.7
  • @types/bcrypt ^5.0.2
  • @types/jest ^29.5.12
  • @types/node ^20.12.2
  • @types/nodemailer ^6.4.14
  • @types/supertest ^6.0.2
  • @typescript-eslint/eslint-plugin ^7.4.0
  • @typescript-eslint/parser ^7.4.0
  • eslint ^9.0.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-prettier ^5.1.3
  • husky ^9.0.11
  • jest ^29.7.0
  • prettier ^3.2.5
  • supertest ^7.0.0
  • ts-jest ^29.1.2
  • ts-loader ^9.5.1
  • ts-node ^10.9.2
  • tsconfig-paths ^4.2.0
  • typescript ^5.4.3

  • Check this box to trigger a request for Renovate to run again on this repository

Fix test e2e

Fix the following e2e tests:

  • change-password.e2e-spec.ts
  • login.e2e-spec.ts
  • users.e2e-spec.ts

jwt stragety question

Hi Tony,

Sorry if I abused your issues. I wanted to ask you a question or send you a message regarding this repo (I guess it will be greate if Github allows user send message to each other)

in the validate function of jwt.strategy.ts
why do you need this.loginService.validateUserByJwt() since the validation is done by jwt, and if it pass, you will receive
the user playload in the validation function, otherwise the framework will throw unauthorized exception directly?

I am in the process of learn it, so maybe I am missing something.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.