Skip to main content

Posts

Showing posts with the label JWT Authetication

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

Part-1 VueJS JWT Auth Cookie - Access Token Usage

In this article, we will implement Vue3 application authentication with the JWT auth cookie. So in this portion, we mainly target access token utilization. To know about Jwt authentication in vuejs like managing token using browser storage then check below mentioned articles. Part-1 Jwt Access Token Auth In Vue3 Part2 Refresh Token In Vue3 HTTP Only JWT Cookie: The HTTP only cookie is only accessible by the server application. Client apps like javascript-based apps can't access the HTTP-only cookie. So if we use authentication with HTTP only JWT cookie then we no need to implement custom logic like adding authorization header or storing token data, etc at our client application. Because once the user authenticated, that auth cookie will be automatically sent to the server by the browser on every API call. Authentication API: To implement JWT cookie authentication we need to set up an API. For that, I had created a mock authentication API(Using the NestJS Server framework). So d

Hot Chocolate GraphQL Custom Authentication Series Using Pure Code First Technique - Part2 - Generating JWT(JSON Web Token) Access Token

Part1  discussed user registration. In this article, we are going to implement logic to generate the JWT access token in the Hot Chocolate GraphQL. Overview On JWT(JSON Web Token): JSON Web Token is a digitally signed and secured token for user validation. The jwt is constructed with 3 informative parts: Header Payload Signature Install JWT NuGet: Package Manager Command: Install-Package System.IdentityModel.Tokens.Jwt -Version 6.9.0 .Net CLI Command: dotnet add package System.IdentityModel.Tokens.Jwt --version 6.9.0 Add Token Settings: While generating the JWT access token, few token-specific settings need to be specified. appsettings.Development.json: "TokenSettings":{ "Issuer":"localhost:5001", "Audience":"js.app.com", "Key":"SomeRandomlyGeneratedStringSomeRandomlyGeneratedString" } The 'Issuer' is like the identification of the server that generated the token. In access token 'iss'

Hot Chocolate GraphQL Custom Authentication Series Using Pure Code First Technique - Part1 - User Registration

About The Series: Using Pure Code First Technique In Hot Chocolate GraphQL, Custom Authentication Series: Part1 User Registration Resolver Part2 Generating JWT Access Token For User Authentication. Part3 Validating JWT Access Token And Different Authorization Techniques Part4 Generating Refresh Token. So this our Part-1 of the series where we are going to create a sample in GraphQL for user registration. SQL Tables: Create 2 tables like 'User' and 'UserRoles'. User Table: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](192) NULL, [LastName] [varchar](192) NULL, [EmailAddress] [varchar](192) NOT NULL, [Password] [varchar](640) NOT NULL, [RefreshToken] [varchar](640) NULL, [RefershTokenExpiration] [datetime] NULL, CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED ( [UserId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY