Skip to main content

Posts

NestJS JWT Auth Cookie Series - Part-3 - Refresh Token

In the  previous article , we understand the steps to generate the JWT token and store it in the cookie. Now here we will understand steps to protect API and also about refresh token. Install passport-jwt NPM Package: We have to create a new jwt passport strategy to validate the jwt token, so we need to install the below packages. Command To Install passport-jwt Packages: npm install --save passport-jwt npm install --save-dev @types/passport-jwt Install And Setup Cookie Parser: To read the cookie in the nestjs application we have to install the below plugin. Command To Install Cookie Parser: $ npm i cookie-parser $ npm i -D @types/cookie-parser Now configure the cookie parser 'main.ts' src/main.ts: Create JWT Passport Strategy: So to apply authentication to API's we have to validate our jwt token, so to do that we need to create a new jwt passport strategy. src/users/jwt.strategy.ts: import { Injectable, UnauthorizedException } from "@nestjs/common"; im

NestJS JWT Auth Cookie Series - Part-2 - Generating Access Token

In this article, we target to generate the jwt authentication and store it in the HttpOnly cookie for user authentication.  Part-1  completely explains implementing user registration in the nestjs application. Implement Logic To Validate User Credentials: First, let's create a model to store the valid user. src/models/current.user.ts: export class CurrentUser { userId: number; firstName: string; lastName: string; email: string; } Now we have to implement the logic for the login endpoint that is to validate user email and password. src/users/users.service.ts: public async validateUserCredentials(email: string, password: string):Promise<CurrentUser> { let user = await this.user.findOne({ email: email }); if (user == null) { return null; } const isValidPassword = await bcrypt.compare(password, user.password); if (!isValidPassword) { return null; } let currentUser = new CurrentUser(); currentUser.userId = user.userId;

NestJS JWT Auth Cookie Series - Part-1 - User Registration

This is the first installment of the NetsJS JWT Auth Cookie Series. In this part our main focus on user registration by the NestJS endpoint. PostgreSQL Database: For this demo, I'm using the free open-source PostgreSQL database. Here I'm going to use the PostgreSQL docker image because it is easy and fast to set up and configure.  Click here to getting started with PostgreSQL docker . Run the following database query to create the 'User' table. CREATE TABLE User( UserId SERIAL PRIMARY KEY NOT NULL, FirstName VARCHAR(200) NULL, LastName VARCHAR(200) NULL, Email VARCHAR(200) NOT NULL, Password VARCHAR(200) NOT NULL, RefreshToken VARCHAR(1000) NULL, RefreshTokenExp DATE NULL ) Create A NestJS App: Let's begin our demo by creating a sample NestJS application. Command To Install NestJS CLI npm i -g @nestjs/cli Command To Create App nest new your_project_name Install ORM And PostgreSQL NPM Packages: ORM packages are essential to install because they prov

Introduction On PostgreSQL Database Using Docker

PostgreSQL is a free open-source relational database management system. In this article, we will learn basic steps to run the PostgreSQL Docker image. Install And Setup Docker: Step1: Download docker into our local system "https://docs.docker.com/desktop/" . Docker was available for all desktop operating systems. Step2: After downloading the docker installer, then install it. Now to run any docker containers(eg: Redis, MongoDB, PostgreSQL, etc) this docker instance we just installed should be active(should be running). Postgres Docker Image: We have to download or pull the Postgres docker image into our local system by running the below command. docker pull postgres Run Postgres Docker Container: After downloading the Postgres image, need to start the container by running the below command. docker run --name mypostgres -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres --name flag to specify the name of our container. -e flag to specify an environmental variabl

GraphQL Subscriptions In Angular Using Apollo Angular Library

In this article, we are going to understand GraphQL Subscriptions in an angular application using the Apollo Angular Library. Graphql Subscription: GraphQL subscriptions are a way to push data from the server to the clients that listen to real-time messages or payload or data from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client, but instead of immediately returning a single response, a result is sent every time a particular event happens on the server. So for this demo, I have created a GraphQL endpoint in .Net5. So I'm going to use that as my server. Publisher: Subscriber: So above 2 images like 'Publisher' and 'Subscriber' are tested using the browser. Now we will implement those publisher and subscriber functionalities into the angular application. Create An Angular Application: Let's begin our demo by creating an angular sample application. Command To Install Angular CLI:(If not insta

A Demo On HotChocolate GraphQL Subscriptions(Pub/Sub) Using Application InMemory Storage

This article will implement a small demo on subscriptions in Hot Chocolate Graphql using the application In-Memory storage. Since our target storage is application In-Memory, so this approach only apt for a single server hosted application. For applications with multiple servers then we have to use Redis Store. GraphQL Subscription Flow: In general, events raising or publishing will be triggered from the GraphQL mutation resolvers. Inside mutation resolvers raise the event to store the data into application in-memory storage. In GraphQL 'Subscriptions' is one of the root definitions like 'Query', Mutation'. So the subscription resolvers will always receive the data from in-memory storage. So the subscription resolvers can also be called subscribers whose job always watches the data send by the raised events. So the event raised from the mutation resolver will be received by all subscribers. Create A .Net5 Web API App: Now let's begin our journey by creat