Skip to main content

Posts

Showing posts with the label JWT Authetication

Part-2 NestJS JWT(JSON Web Token) Authentication(Refresh Token Implementation)

In  Part-1  we have done complete implementation on NestJS application authentication by creating the user access token. Now we are going to explore the implementation of the refresh token. We will continue to work on the sample app we have used in  Part-1 . Refresh Token Flow: Refresh Token is a random string key that will be created along with the JWT access token and return to the valid client on successful logging in. Now for all subsequent requests will use the access token, but the access token is a short-lived token where as refresh token lives more time than the access token. On the expiration of the access token, the user instead of authenticating himself again passing his user name and password, the user can send the refresh token. The server on receiving a refresh token, first it validates against the storage(database, cache, etc). For a valid refresh token server will create a new access token and refresh token(like when authenticate using user name and password) return it

Part-1 NestJS JWT(Json Web Token) Authentication(Access Token Implementation)

In this article, we are going to explore the implementation steps of JWT(JSON Web Token) authentication in the NestJS application. In this process of authentication, we going to use the 'passport' library(nodejs library) where we write simple customizable authentication. Create A Sample NestJS Application: Let's understand step by step implementation authentication in NestJs application, so let's begin our journey by creating a sample application. Command To Install CLI: npm i -g @nestjs/cli Command To Create NestJS App: nest new your_project_name Create Users Model: Create a 'User' model that represents the table. For now, let's create a simple class in the upcoming steps we make it compatible to communicate with the database. src/users/users.ts: export class User{ id:number; userName: string; password: string; } Create A UsersService: The 'UsersService' is a logical container for our 'Users' data. For now, just mock the user&