Hello everyone! I am going to describe the JWT refresh token generator in NestJS using a TODO application. We are using -

TypeORM
Swagger
PostgresSQL
PGadmin4
JWT
Docker

You can check the initial stage of this article where this application is described- A simple todo application & git repo

Update .env file

JWT_ACCESS_TOKEN_SECRET=anyKey
JWT_ACCESS_TOKEN_EXPIRATION_TIME=900
JWT_REFRESH_TOKEN_SECRET=anyRefreshKey
JWT_REFRESH_TOKEN_EXPIRATION_TIME=28800

Update config/default.yml file

jwt: 
expiresIn: 600
refreshExpiresIn: 120

Update config/development.yml file

jwt:
secret: 'anyKey'
refreshSecret: 'noKey'

Update docker-compose.yml file under nestjs service

-JWT_ACCESS_TOKEN_EXPIRATION_TIME=${JWT_ACCESS_TOKEN_EXPIRATION_TIME}
- JWT_ACCESS_TOKEN_SECRET=${JWT_ACCESS_TOKEN_SECRET}
- JWT_REFRESH_TOKEN_SECRET=${JWT_REFRESH_TOKEN_SECRET}
- JWT_REFRESH_TOKEN_EXPIRATION_TIME=${JWT_REFRESH_TOKEN_EXPIRATION_TIME}

Update src/auth/jwt-strategy.ts file

Create src/auth/jwt-refresh-strategy.ts file…


N.B. Please finish the Part-1 first to understand rest of the tutorial

So our authentication is working properly. Let’s update a user’s profile by hitting /api/user — PATCH method. On successful update, you should able to see the below image-

To view the user information, use api/user — GET method & your screen will be -


NestJs & Docker Logo
NestJs & Docker Logo
NestJS & Docker

Aloha !!! This tutorial will be very basic of a development stack and required a basic understanding of Docker. But if you haven’t, don’t worry you can follow along with the instruction and will find it very easy. So let’s Jump in.

What is NestJS?
NestJS is a framework for building efficient Node.js server-side application with fully supports TypeScript and also makes use of Server frameworks Ex. Express.

What is TypeORM?
TypeORM is object-relational mapper library and can be used with TypeScript & JavaScript.

What is PostgreSQL ?
PostgreSQL is a powerful and advanced open-source relational database.

What is Swagger…

Tushar Roy Chowdhury

I am passionate programmer and always keep eyes on new technologies and scrutinize until get into a structure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store