Skip to content

About JwtService utilities module based on the nestjs/jwt package Authentication using NestJS & JWT Token, JwtService and cookies as access token & refresh token.

Notifications You must be signed in to change notification settings

vinodnextcoder/nestjs-mongoose-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e16ca29 Â· Feb 14, 2024
Dec 29, 2023
Dec 28, 2023
Dec 28, 2023
Dec 26, 2023
Feb 10, 2024
Dec 26, 2023
Dec 25, 2023
Dec 28, 2023
Dec 11, 2023
Nov 1, 2023
Nov 1, 2023
Dec 26, 2023
Feb 14, 2024
Dec 11, 2023
Dec 29, 2023
Feb 10, 2024
Dec 11, 2023
Dec 11, 2023

Repository files navigation

NestJS JWT Service with Cookie and Authorization

This repository contains a NestJS application showcasing JWT (JSON Web Token) authentication using cookies and authorization implementation.

Features

  • JWT Authentication: Implementation of JWT-based authentication using NestJS.
  • Cookie-based Tokens: Store JWT tokens in HTTP-only cookies for secure communication.
  • Authorization Middleware: Middleware for protecting routes and verifying user authorization.
  • Winston Logger Winston is designed to be a simple and universal logging library with support for multiple transports.

Prerequisites

Ensure you have the following installed before running the application:

  • Node.js (>= 12.x)
  • npm
  • MongoDB (or any preferred database)

Installation

  1. Clone the repository:

    https://github.com/vinodnextcoder/nestjs-mongoose-boilerplate.git
  2. Install dependencies:

    cd nestjs-mongoose-boilerplat
    npm i -g cross-env
    npm install
  3. Set up environment variables:

    Create a .env file in the root directory and provide necessary environment variables (check .env.example for reference).

Configuration

  • Update the JWT_KEY and other necessary environment variables in the .env file.

Mongodb url update as per your config in env file

  • MONGODB_URI=mongodb://localhost:27017/test

    or

  • MONGODB_URI=mongodb://0.0.0.0:27017/test

Usage

  1. Start the application:
# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Swagger documentation for nestjs-mongoose-jwt

Running

You can get token from cookie response

Once the application is running you can visit [http://localhost:3000/api](http://localhost:3000/api) to see the Swagger interface.
  1. Access the API:

    Use Postman, curl, or any REST client to interact with the API endpoints:

    • Register a user: POST /v1/users
    • Login: POST /auth/login
    • Access protected routes: Use JWT token in the Authorization header or verify the cookie.

Endpoints

  • POST /v1/users: Register a new user.
  • POST /auth/login: Log in and receive a JWT token.
  • GET /v1/users: Access a protected route (requires authentication).

Author

👤 Vinod

Contributing

Contributions are welcome! If you find any issues or want to enhance the application, feel free to submit a pull request.

husky added you skip in commint using below command

git commit -m "commit msg" --no-verify

License

This project is licensed under the MIT License.