https://github.com/softdev629/codevo-nodejs-jwt-auth
JWT Authentication and Authorization
https://github.com/softdev629/codevo-nodejs-jwt-auth
bycript cookie express jwt-authentication mongodb mongoose nodejs redis typegoose typescript
Last synced: 5 months ago
JSON representation
JWT Authentication and Authorization
- Host: GitHub
- URL: https://github.com/softdev629/codevo-nodejs-jwt-auth
- Owner: softdev629
- Created: 2024-04-01T15:50:15.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-01T21:06:02.000Z (almost 2 years ago)
- Last Synced: 2025-01-07T08:17:06.732Z (about 1 year ago)
- Topics: bycript, cookie, express, jwt-authentication, mongodb, mongoose, nodejs, redis, typegoose, typescript
- Language: TypeScript
- Homepage:
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readMe.md
Awesome Lists containing this project
README
# JWT Authentication and Authorization
## 1. Node.js + TypeScript + MongoDB: JWT Authentication
In this article, you'll learn how to add JSON Web Token (JWT) Authentication to your Node.js app with TypeScript, MongoDB, Mongoose, Typegoose, Docker, Redis, and Zod.

### Topics Covered
- Setup Development Environment (Optional)
- Download and Install Node.js
- Download and Install Docker
- Download and Install MongoDB Compass
- Node.js, Redis, MongoDB, Typegoose, Docker: JWT Authentication example
- JWT Authentication Flow with Redis, MongoDB, and Node.js
- Project Structure
- Project Setup
- Initialize a Node.js Project with TypeScript
- Install the Required Libraries
- Initialize and Start the Express Server
- Setting up Redis and MongoDB with Docker Compose
- Connecting to the MongoDB Docker Container with Mongoose
- Connecting to Redis Docker Container
- Creating the Database Schema with Typegoose
- How to Generate Private and Public keys for JWT Authentication
- Define Middleware to Sign and Verify JWTs
- Define a Custom Error Handler in Express
- Define the Zod Validation Schema
- Create a Middleware to Validate the User Inputs
- Create a Service to Communicate with the Database
- Create the Authentication Controller
- Create the User Controller to Test Authorization
- Define a function to deserialize the User
- Define a function to check if the user is logged in
- Define a Middleware to Restrict Unauthorized Access
- Create the Authentication Routes
- Update the app.ts file to use the route
- Testing the JWT Authentication Rest API
- Register users
- Login user
- Get Currently Logged in User's Credentials
- Admin Get All Users
## 2. Node.js + TypeScript + MongoDB: JWT Refresh Token
In this article, you'll learn how to implement JWT Authentication with an Access and a Refresh token using Node.js, TypeScript, MongoDB, Redis, and Docker.

### Topics Covered
- JWT Refresh Token with Node.js, TypeScript, and MongoDB Overview
- JWT Refresh Token Implementation Flow
- Generate Public and Private Keys for the Token
- Update Environment Variables in Config
- Update the Sign and Verify JWT Utility Functions
- Update the Sign Token Service Function
- Update the Login Controller
- Create a Controller to Refresh Access Token
- Create a Controller to Logout User
- Update the Authentication Routes
- Update app.ts