This repository contains a NestJS application showcasing JWT (JSON Web Token) authentication using cookies and authorization implementation.
- 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.
Ensure you have the following installed before running the application:
- Node.js (>= 12.x)
- npm
- MongoDB (or any preferred database)
-
Clone the repository:
https://github.com/vinodnextcoder/nestjs-mongoose-boilerplate.git
-
Install dependencies:
cd nestjs-mongoose-boilerplat npm i -g cross-env npm install
-
Set up environment variables:
Create a
.env
file in the root directory and provide necessary environment variables (check.env.example
for reference).
- Update the
JWT_KEY
and other necessary environment variables in the.env
file.
- MONGODB_URI=mongodb://localhost:27017/test
- MONGODB_URI=mongodb://0.0.0.0:27017/test
- Start the application:
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
Once the application is running you can visit [http://localhost:3000/api](http://localhost:3000/api) to see the Swagger interface.
-
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.
- Register a user:
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).
👤 Vinod
- Github: @vinodnextcoder
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
This project is licensed under the MIT License.