NestJs REST API with MongoDB #4 - Authentication, Login/Sign Up, assign JWT and more

Coding With Abbas
27 Jan 202322:52

Summary

TLDRThis tutorial video guides viewers through implementing authentication in a REST API using Nest.js. It covers setting up the Passport library, creating authentication modules, and generating JWT tokens for user login and registration. The video also demonstrates how to validate user credentials and handle errors. Viewers are encouraged to watch the next video for securing routes and additional authentication features.

Takeaways

  • πŸ“š In previous videos, we created a simple REST API, added search, pagination, and handled errors.
  • πŸ” This video focuses on implementing authentication in our API with two parts: creating login and register routes and providing JWT tokens.
  • πŸ“– The next video will cover authorizing users and protecting routes from unauthenticated access.
  • πŸ› οΈ We use Nest.js for our API and configure authentication with the Passport library.
  • πŸ“¦ Several packages need to be installed, including nest.js/passport and JWT modules.
  • πŸ”§ We create an auth module, controller, and service, and configure Passport and JWT in the auth module.
  • πŸ’Ύ The user schema is defined with fields for email and password, ensuring email uniqueness and adding timestamps.
  • πŸ”‘ JWT configuration includes setting secret and expiration time using environment variables.
  • πŸš€ Auth service handles user registration with hashed passwords and returns a JWT token.
  • πŸ§ͺ Validation checks are performed during login, and JWT tokens are generated and returned upon successful login.
  • πŸ” Detailed steps include installing necessary packages, setting up schemas, configuring modules, and creating routes for login and signup.
  • 🎬 The next video will continue with protecting routes using JWT strategy and saving the user in the book.

Q & A

  • What is the main focus of the video?

    -The video focuses on implementing authentication in a REST API using Nest.js, including creating login and register routes and issuing JWT tokens to users.

  • Why is the Passport library used in Nest.js for authentication?

    -Nest.js uses the Passport library because it is a popular library for Node.js used for authentication, and it integrates well with Nest.js through the 'nestjs/passport' package.

  • What are the packages needed to be installed for implementing authentication in the video?

    -The packages needed include 'passport', 'nestjs/passport', '@nestjs/jwt', and 'bcryptjs' for hashing passwords.

  • How does the video guide the creation of the authentication module?

    -The video instructs the viewer to use the 'nest g module auth' command to generate the authentication module and then proceed to create a controller and service for it.

  • What is the purpose of creating a 'schemas' folder and a 'user.schema.ts' file?

    -The 'schemas' folder and 'user.schema.ts' file are created to define the user's data structure, including fields like email and password, and to ensure that the email is unique.

  • Why is the 'bcryptjs' package used in the video?

    -The 'bcryptjs' package is used for hashing passwords before saving them to the database, which is a security measure to protect user data.

  • How does the video handle the creation of DTOs (Data Transfer Objects) for signup and login?

    -The video demonstrates creating 'signup.dto.ts' and 'login.dto.ts' files to define the data structure for the signup and login requests, including validation rules for the email and password fields.

  • What is the role of the JWT token in the authentication process shown in the video?

    -The JWT token is issued to the user upon successful signup or login, serving as a secure way to authenticate the user's identity for subsequent requests.

  • How does the video ensure the security of the JWT token?

    -The video sets an expiry time for the JWT token and uses a strong secret key defined in the environment variables to sign the token, enhancing its security.

  • What is the next step after the authentication implementation covered in the video?

    -The next step, as mentioned in the video, is to authorize users and protect routes from unauthenticated users in the subsequent video.

  • How can viewers access the source code and further learning materials mentioned in the video?

    -Viewers can find the source code and additional learning materials, such as a Udemy course link, in the description of the video.

Outlines

00:00

πŸ“š Introduction to API Authentication Implementation

The video begins with an introduction to the task at hand, which is to implement authentication in a previously developed REST API. The speaker outlines a two-part video series, with the current part focusing on setting up the authentication process, including login and registration routes, and issuing JWT tokens to users. The second part will cover user authorization and route protection. Nest.js is highlighted as the framework of choice, utilizing the Passport library for authentication, with specific packages to be installed for the task.

05:01

πŸ›  Setting Up Passport and JWT for Authentication

The speaker proceeds to demonstrate the setup of the Passport and JWT modules for authentication within the Nest.js framework. This includes installing necessary packages, configuring the auth module to use Passport with JWT strategy, and setting up environment variables for the JWT secret and expiry time. The config service is used to access these variables, and the user schema is registered to integrate with the authentication process.

10:03

πŸ”‘ User Registration and Password Hashing

The video script details the creation of a user registration process. This involves setting up a user schema with email and password fields, ensuring the email is unique. The speaker uses bcryptjs for password hashing before saving the user to the database. After successful registration, the system assigns a JWT token to the user. The process includes creating DTOs (Data Transfer Objects) for sign up and login, and demonstrating the validation of user input.

15:03

πŸ”’ User Login Route Creation and Testing

The speaker moves on to creating a login route, which involves validating user credentials and issuing a JWT token upon successful authentication. The login DTO is created with email and password fields. The script describes how to check if the user exists and validate the password using bcrypt's compare function. If the credentials are correct, a token is issued; otherwise, an unauthorized exception is thrown. The speaker tests the login route using Postman, demonstrating the process and expected outcomes.

20:04

πŸš€ Conclusion and Future Steps in Authentication

In conclusion, the speaker summarizes the successful creation of sign up and login routes, including the assignment of JWT tokens to users. The script mentions plans for the next video, which will focus on protecting routes from unauthenticated users and saving user data in a book (likely a typo for 'database'). The speaker invites viewers to ask questions in the comments and provides a link to a related Udemy course and source code in the video description.

Mindmap

Keywords

πŸ’‘REST API

REST API stands for Representational State Transfer Application Programming Interface, which is a set of rules for structuring URLs, request-response formats, and actions for creating web services. In the video, the speaker discusses enhancing a simple REST API by adding authentication, which is a crucial aspect of web service security, ensuring that only authorized users can access the API's resources.

πŸ’‘Authentication

Authentication is the process of verifying the identity of a user or device. It is a critical component in securing applications and services. In the context of the video, the speaker is implementing authentication in a REST API using JWT tokens, which allows users to be identified and authorized to access certain routes or resources.

πŸ’‘JWT

JWT stands for JSON Web Token, which is a compact, URL-safe means of representing claims to be transferred between two parties. It is often used for authentication purposes, as it allows information to be encoded and verified in a way that is secure between parties. In the video, the speaker explains how to issue JWT tokens to users upon successful login or registration.

πŸ’‘Nest.js

Nest.js is a JavaScript framework for building efficient, reliable, and scalable server-side applications. It uses modern JavaScript (or TypeScript) and is built with the Express engine under the hood. The video script revolves around using Nest.js for creating a secure API, leveraging its modules and libraries for authentication.

πŸ’‘Passport

Passport is an authentication middleware for Node.js. It is designed to be unobtrusive, flexible, and minimalistic, making it easy to integrate with any Express-based web application. In the video, Passport is mentioned as the underlying library that Nest.js uses for handling authentication.

πŸ’‘Schema

In the context of databases and data modeling, a schema is a collection of rules that define the structure of the data and the relationships between different data elements. In the script, the speaker creates a user schema using Mongoose, which is a MongoDB object modeling tool designed to work in an asynchronous environment.

πŸ’‘Mongoose

Mongoose is a MongoDB object modeling tool that provides a straightforward way to create models and interact with the MongoDB database. It is used in the video to define a user schema and to interact with the database for storing user data and performing operations like hashing passwords.

πŸ’‘Bcrypt

Bcrypt is a password-hashing function designed to be slow, making it resistant to brute-force search attacks. In the video, the speaker uses bcrypt to hash passwords before storing them in the database, ensuring that even if the data is compromised, the passwords are not easily deciphered.

πŸ’‘Environment Variables

Environment variables are a set of dynamic values that can affect the way running processes will behave on a computer. In the video, the speaker sets environment variables for 'JWT_SECRET' and 'JWT_EXPIRE' to store sensitive information like the secret key for JWT tokens and the expiration time for those tokens.

πŸ’‘DTO (Data Transfer Object)

A Data Transfer Object (DTO) is an object that carries data between processes, such as between a server and a client. In the video, DTOs are used for sign-up and login operations, allowing the API to receive and transfer data like user names, emails, and passwords.

πŸ’‘Unauthorized Exception

An unauthorized exception is thrown when an operation is attempted without proper authentication. In the script, the speaker uses unauthorized exceptions to handle cases where login credentials do not match or are missing, ensuring that only authenticated users can access protected routes.

Highlights

Introduction to implementing authentication in a REST API using Nest.js

Overview of two-part video series on authentication, starting with login and registration routes

Explanation of using passport library for authentication in Nest.js applications

Instructions on installing necessary packages for authentication with Nest.js

Creation of an authentication module using Nest.js CLI commands

Generation of controller and service without spec files for the auth module

Configuration of passport in the auth module and setup for login and sign up routes

Creation of a user schema with Mongoose for handling user data and timestamps

Use of unique property to ensure email uniqueness in the user schema

Registration of user schema and setup of passport with JWT strategy

Installation and setup of JWT module for token generation

Configuration of JWT secret and expiry time using environment variables

Implementation of user registration service with password hashing

Use of bcrypt for secure password hashing before saving to the database

Creation of DTOs (Data Transfer Objects) for sign up and login processes

Validation of user input using decorators in DTOs for email and password

Development of the sign up endpoint in the auth controller

Testing the sign up route using Postman and observing validation responses

Demonstration of successful user creation and JWT token generation

Explanation of JWT token contents using jwt.io to inspect the token

Setup of the login route with validation of user credentials

Testing the login route and verifying the correct assignment of JWT tokens

Upcoming continuation of the series focusing on protecting routes and saving users in a book

Invitation for questions and reference to a Udemy course for further learning

Transcripts

play00:00

welcome back to this channel so in our

play00:01

previous videos we have created a simple

play00:03

rest API we have added the search

play00:06

pagination and we have handled our

play00:07

errors and now in this video we will

play00:09

implement the authentication uh in our

play00:12

API now there are two parts of this

play00:14

video in the first part we will add the

play00:16

authentication we will create our login

play00:17

route register route and we will give

play00:20

the JWT token to the user and in our

play00:23

next video we will authorize our users

play00:25

we will protect our routes from

play00:27

unauthenticated users so make sure to

play00:29

watch all uh the videos so in this video

play00:32

we will implement the authentication so

play00:34

I am on the documentation of nest.js you

play00:36

can see that here under security we have

play00:38

authentication so nest.js uses passport

play00:41

Library if you don't know password is a

play00:42

very popular library for node.js that is

play00:45

used for authentication so nest.js uses

play00:48

the passport package so you have to also

play00:50

use that and you can see here we have a

play00:52

package that is nest.js slash passport

play00:55

that is configured for the nest.js API

play00:57

so we have to use these packages

play00:59

so if you scroll down we have to install

play01:01

all these packages in order to work I

play01:03

simply copy it from here copy that and I

play01:06

go back and let's install all these

play01:09

packages so I simply paste it here and

play01:12

let's install them let's go back and

play01:14

copy this as well and install it

play01:20

okay so our packages are installed

play01:22

successfully now I have to create our

play01:24

module for authentication so I type here

play01:26

Nest G module and the name of the module

play01:29

is going to be auth so I will generate

play01:31

the module

play01:32

now let's create our controller and

play01:34

service for the module so I type here

play01:36

Nest G

play01:37

controller auth dash dash no spec so

play01:41

that it will not create any testing file

play01:44

we will handle the testing in our

play01:47

upcoming videos so I will create my

play01:49

controller

play01:51

then I will create my service file here

play01:57

and that is it now let's work on our

play02:00

application

play02:01

so first of all let's configure our

play02:04

passport in our auth module and then we

play02:06

will create our two routes for the login

play02:08

and sign up okay

play02:10

so I go to source and the auth we have

play02:13

this auth module.ts and here we have to

play02:16

configure our

play02:18

um and let's actually before that create

play02:20

our schema so in the auth create a new

play02:23

folder that is called schemas create a

play02:26

new file

play02:27

uh I call it user dot schema dot TS

play02:32

let's create our schema first I close

play02:34

this one so here first of all I use here

play02:38

schema and I will

play02:40

pass the option that time stamps to true

play02:44

oops so that it will add the time

play02:47

automatically when we will create our

play02:49

user so you export here

play02:54

a class I have discussed all about these

play02:57

in my previous video If you haven't

play02:58

watched that you can watch that also as

play03:00

well then it will make more sense now

play03:02

let's add our Fields here so I add here

play03:05

at prop decorator

play03:07

in which we can pass further options for

play03:09

these fields but I will leave it empty

play03:12

string and then we have here

play03:15

two fields one is the email and one is

play03:20

the

play03:22

password

play03:24

so in the props I will simply type here

play03:26

some options like unique

play03:28

to make sure that the email is unique

play03:30

and if the email is not unique we will

play03:33

simply

play03:34

send back

play03:36

duplicate

play03:39

email entered like this simply say this

play03:43

one and then we have to export here so

play03:46

export

play03:47

const user schema

play03:51

so I use a schema Factory

play03:56

dot create for class and pass in here my

play03:59

user okay so now our schema is ready

play04:02

here long let's

play04:04

um actually close this one go to our

play04:06

auth module now let's register our

play04:08

schema as well and also set up our

play04:10

passport here so I create here Imports

play04:13

array

play04:15

in that I will first of all set up here

play04:18

the Mongoose module

play04:21

dot for feature and in that we have to

play04:24

pass the array of our models so the

play04:27

first is going to be user

play04:30

and schema is going to be the user

play04:33

schema like this and now we have to

play04:36

configure our password module as I have

play04:39

explained before nest.js uses the

play04:40

password for the authentication so I use

play04:43

here passport uh password module and I

play04:46

have to import that from nest.js slash

play04:48

passport that we have installed before

play04:50

okay then register okay now in the

play04:54

register we have to pass here one option

play04:56

that is default strategy that with

play04:58

strategy we want to use and we want to

play05:01

use the JWT strategy okay so now we have

play05:03

registered our password module now let's

play05:06

uh register our JWT module so JWT

play05:11

uh module and I think so

play05:14

I have to install that as well so I type

play05:17

here npmi at nest.js slash JWT let's

play05:23

install this package also okay so

play05:25

package is installed successfully now I

play05:27

type here password module I have to

play05:30

import that from uh nest.js diesel

play05:33

Beauty dot register async so we will use

play05:36

here register async we can use here uh

play05:39

the simple register function but we have

play05:41

to also use the config variable inside

play05:46

it so that's why we have to use here

play05:48

register async so let me show you how it

play05:52

will work

play05:53

now here we have first of all inject our

play05:55

config service because we have to use

play05:57

here two config variables one is a DWT

play06:00

secret and one is the JWT expired if I

play06:03

go to my EnV file let's register here

play06:06

first of all

play06:07

JWT

play06:10

expire

play06:11

and also JWT

play06:15

Secret

play06:18

so make sure that you put here a strong

play06:20

secret I will use here my name

play06:22

accordingly the bus and then the JWT

play06:25

expire so this is the expiry time of the

play06:28

token so I type here like three days now

play06:31

the expiry time of our jw3 token is

play06:34

going to be three days so I will Define

play06:36

all my two variables here close it now

play06:39

we have to use that here and before that

play06:41

we have to inject our config service so

play06:44

I use here inject

play06:46

on the inject I will use here config

play06:49

service and if you don't use here like

play06:52

this you will not be able to access here

play06:56

process.env.jwt secret it will throw

play06:58

undefined so we have to use that in this

play07:00

way we have to inject our config service

play07:02

then we have to use data function that

play07:05

is called use Factory

play07:07

and now

play07:09

in that we will get access to the config

play07:11

so the config and type is going to be

play07:14

config service

play07:16

like this

play07:18

now let's

play07:20

return from here an object in that we

play07:23

have to pass two things first of all the

play07:25

secret

play07:26

okay now we have defined security number

play07:29

EMV file so I will use here config

play07:32

which is this one

play07:34

that will give us access to the EnV

play07:37

variables so dot get

play07:39

and I use here JWT

play07:43

underscore

play07:45

secret and one more

play07:48

that is the uh

play07:51

sign in options and that is going to be

play07:54

JWT

play07:56

JWT

play07:58

expires I think so

play08:01

expires

play08:03

save it so this is the variable that we

play08:05

have to use and let's also give here

play08:08

type that is going to be

play08:11

oops

play08:13

string and this is going to be also

play08:17

string

play08:18

but it can be a number also because if

play08:21

user only pass here like 5 or 6 or 10

play08:24

like for the minutes or

play08:27

um other numbers so we have to allow

play08:28

that also

play08:29

so it is going to be either a string or

play08:33

number

play08:34

and if you have to actually cut it from

play08:36

here create here an object in that we

play08:39

will have to pass here expires in and

play08:41

then we will use like this Simply Save

play08:43

it and now it will set up our password

play08:46

module JWT module and also we have

play08:49

registered our user schema here simply

play08:52

we have to register our password module

play08:54

like this pass and here our default

play08:55

stats data is going to be JWT then we

play08:57

will use here our JWT module we have to

play09:00

register it so I will inject my config

play09:03

service because I want to access my

play09:05

config variable inside it so I have to

play09:08

pass here two things first of all the

play09:09

secret which is going to be this and

play09:11

then in the sign options I have to pass

play09:12

the expiry date of that token which is

play09:15

this one that we have defined in our

play09:17

config file now let's remove this one

play09:20

and go to our

play09:23

here in the service and create our

play09:25

routes for the login and sign up so here

play09:28

first of all in the auth service I will

play09:30

create my Constructor we have to inject

play09:32

our model so

play09:35

we'll simply type here add

play09:38

inject

play09:40

model and I have to pause here the user

play09:43

dot name okay and then I will call it

play09:50

private user model that is going to be

play09:54

model and type is going to be user okay

play09:59

and I have to import the model from

play10:02

Mongoose like this

play10:04

and then here after the Constructor I

play10:06

have to register my user so I type here

play10:08

async

play10:09

I call it sign up

play10:11

and in that I have to pass Here sign up

play10:13

dto that I will create in a minute

play10:16

and

play10:18

I have to first of all get

play10:22

name

play10:24

email and then the password

play10:28

from the sign up dto okay and then I

play10:32

have to Hash my password before saving

play10:34

any database so I have to also install

play10:36

one more package that is B grip JS npm I

play10:40

B grip

play10:42

Js

play10:43

I will install it first of all

play10:47

and then I type here const hashed

play10:52

password is going to be a weight

play10:55

B Crypt

play10:59

dot hash and we have to also import that

play11:03

so import all As

play11:06

B grip from

play11:10

B group Js so B grip dot hash

play11:15

um now in that I have to pass in here

play11:17

the password

play11:19

then this old value type here 10 which

play11:21

is also a default value so now we have

play11:24

here our hash password we have to save

play11:25

the user in the database so simply type

play11:27

here const user is going to be await

play11:30

this dot user model dot create

play11:35

and in that I will simply pass in here

play11:37

name

play11:39

email and then the password that is

play11:42

going to be hashed password like this

play11:45

okay and now you can also return the

play11:48

user from here but I have to assign JWT

play11:51

token to the user so that's why I will

play11:53

use here A Sign function that will

play11:56

assign uh JW token to that user how we

play12:00

can do that first of all we have to use

play12:01

here

play12:03

create here a variable so private that

play12:06

is going to be JWT service

play12:09

and the type is going to be jwp service

play12:11

that I have to import from nas JS JWT

play12:14

okay I have to put in here comma

play12:18

and now I will simply type your const I

play12:21

have to create a token so token is going

play12:23

to be this dot JWT service DOT sign so

play12:27

the sign function help us to generate

play12:30

our jwd token so on this sign we have to

play12:33

pass the payload so what is the payload

play12:35

payload is the data that we want to save

play12:37

in the token so we want to save the ID

play12:39

of the user so type here ID and the

play12:42

value is going to be user dot underscore

play12:45

ID okay now we have to return the token

play12:48

from here isomer type here return

play12:51

token okay and the return type of this

play12:54

is going to be

play12:57

I type here promise oops

play13:01

and

play13:03

that is going to be an object

play13:05

that contains token and type is going to

play13:08

be string

play13:09

okay

play13:10

and let's actually first of all create

play13:12

our signup dto I go to the auth

play13:15

create here dto

play13:17

and if you don't know about video and

play13:19

all this stuff I have discussed all that

play13:22

in my previous videos make sure to watch

play13:24

those videos if you don't know so I

play13:27

created a new file that is sign

play13:30

up dot dto.ts so I actually just go to

play13:34

the book dto to save some time and copy

play13:37

uh this from here

play13:40

copy

play13:41

put that here I don't need here

play13:45

this part

play13:48

not in here this

play13:50

so the first thing is going to be the

play13:52

name of the user so that is going to be

play13:54

string and it is not empty then we have

play13:56

here the email that is going to be is

play14:00

email decorator and then I pass in here

play14:06

um the option

play14:08

I type here message

play14:15

please enter

play14:17

correct email and this is going to be

play14:20

password

play14:23

and I can also Define one more decorator

play14:25

here that is minimum length so main

play14:27

length I type here 6 that minimum length

play14:30

of the password is going to be 6. I

play14:33

remove here is number from here and also

play14:35

this enum

play14:37

and that is

play14:40

sign up dto Simply Save this one

play14:42

and also I will simply copy that from

play14:44

here create a new dto for the login so

play14:47

login dot dto.ts put that here and here

play14:52

we need here only email and the password

play14:54

just simply remove this one and that is

play14:56

it and this is

play14:58

uh login video Simply Save this one we

play15:01

have created our dtos I close this one

play15:03

this one and here I give the type that

play15:05

is going to be

play15:07

sign up dto okay so now let's create our

play15:11

controller for this signup I go to my

play15:13

auth.controller.ts and then here I will

play15:16

create my Constructor

play15:19

and in that I will simply create add

play15:21

here my

play15:22

private variable that is going to be

play15:25

auth service if you don't know private

play15:27

variable can only be accessed inside a

play15:30

class if you don't know about the o so

play15:32

that's why we create here private

play15:33

variable so that it can only be accessed

play15:35

inside the class okay so auth service

play15:40

and type is going to be auth service

play15:42

like this and then that is going to be a

play15:46

post

play15:47

request

play15:48

I type here slash sign up so it is going

play15:51

to be slash auth slash sign up and then

play15:54

here

play15:56

I name the function

play15:58

and

play16:00

from the body I have to get my sign up

play16:04

dto

play16:06

okay so sign up

play16:11

dto

play16:12

and then

play16:14

it will return a promise

play16:17

and it

play16:19

object we have token type is going to be

play16:22

string

play16:23

and in that I will simply return this

play16:28

dot auth service DOT sign up and pass in

play16:32

here the sign up dto

play16:35

sign up dto

play16:41

like this and oops that is only sign

play16:44

update you

play16:46

okay so now we will pass the sign up dto

play16:49

which is the body in the sign up and it

play16:51

will HD password to save this on the

play16:54

database and assign the token to the

play16:55

user now let's test our route so I

play16:58

restart my server here and

play17:00

[Music]

play17:02

okay so there is not any error let's go

play17:05

to the postman

play17:07

I simply copy this from here this is

play17:10

going to be a post request that is going

play17:12

to be slash auth slash

play17:15

sign up in the body row and let me type

play17:19

here the data

play17:23

and here you can see that I have passed

play17:25

here incorrect email and the password is

play17:27

also just five characters and if I click

play17:30

on send from here you will see that

play17:32

please enter correct email address and

play17:34

password must be longer or equal to 6

play17:36

characters if I type here at

play17:39

now I will get only password error so I

play17:41

type here 6.

play17:43

so validation is working properly now if

play17:45

I click on send from here you can see

play17:48

that we get back here our token that

play17:50

this means that user is created

play17:52

successfully and this is 201 created if

play17:55

I go to a website that is JWT dot IO

play17:59

here we can actually see what is inside

play18:01

the token so I simply go back uh and

play18:04

copy this token from here

play18:07

and here if I paste it you can see that

play18:11

here we have the ID of the user here

play18:14

okay so our token is created

play18:16

successfully and we have successfully

play18:17

saved our user also now let's create our

play18:20

second route for the login

play18:22

so I go back in the controller

play18:25

sorting the service and then here

play18:28

type here async login now in the login I

play18:32

have to pass here login detail that will

play18:34

contain email and the password of the

play18:35

user so dto that is going to be

play18:38

login dto and it will also return this

play18:42

token

play18:44

okay

play18:47

and now first of all I need to get here

play18:55

the

play18:56

email and then the password from the

play18:59

login dto

play19:01

and now we have to first of all check

play19:02

that if the user exists or not so I

play19:06

simply type here const user is equal to

play19:09

await this dot user model dot find one

play19:15

and in that we will pass

play19:16

the email so we'll check that if the

play19:19

user exists with this email or not and I

play19:22

check that if

play19:24

not user we will simply throw new

play19:29

unauthorized exception

play19:32

and it is going to be invalid

play19:37

invalid email or

play19:40

password

play19:41

okay and if the user exists with that

play19:45

email now we have to validate the

play19:46

password so I type here const is

play19:49

password

play19:52

matched I will use here bcrypt

play19:56

bcrypt has a function that is called

play19:58

compare in the compare we have to pass

play20:00

first of all the password that user has

play20:03

passed in the body so I type here the

play20:06

password

play20:08

then the second password which is saved

play20:11

in the database which is the hashed

play20:13

password so that is going to be user Dot

play20:17

password okay again I checked that if

play20:23

not password match then we will throw

play20:25

this unauthorized exception

play20:28

otherwise we have to assign the token so

play20:30

I simply copy this from here and if the

play20:33

email is ranked password is correct then

play20:35

we will give back the token to the user

play20:37

like this

play20:38

Simply Save It Now go to alt controller

play20:40

and I copy this from here

play20:44

and let's make it get pass in here login

play20:49

I also have to import it

play20:52

that is going to be login

play20:54

and that is

play20:59

login

play21:05

um

play21:07

Dot Login okay and now if I simply save

play21:11

it now let's test it out if I go to my

play21:13

Postman

play21:15

and I copy this from here

play21:19

that is going to be login and the body

play21:22

row

play21:26

um I pass this

play21:28

so if I type here wrong password like

play21:30

this click on oops I have to remove this

play21:33

name from here click on send you can see

play21:36

that invalid email or password if I type

play21:39

here correct password but wrong email

play21:42

send then invalid email or password and

play21:45

if I remove this from here click on send

play21:48

you can see that we get back here our

play21:49

token

play21:51

and if I copy it from here go back and

play21:54

put that here you can see that we have

play21:57

our ID of the user did open so now we

play22:00

have successfully created a two routes

play22:02

one is for sign up and one is for the

play22:04

login and we are also assigning the JWT

play22:08

token to the user and in my next video I

play22:11

will I will basically protect my routes

play22:13

from the unauthenticated users and also

play22:16

I will save the user uh in the book okay

play22:20

so I will handle that in my next video I

play22:22

hope that you understand if you have any

play22:24

question you can definitely post your

play22:26

question in the comment section and if

play22:28

you want to check out my udemy course

play22:29

about nestls I will also add the link in

play22:32

the description of this video

play22:34

so you can also find the source code in

play22:36

the description of this video now in my

play22:38

next video I will continue with the

play22:40

authentication and protect my routes

play22:42

from the unauthenticated users we will

play22:44

create our JWT strategy

play22:46

okay and then we also save our user in

play22:49

the book all right so I will see you in

play22:51

the next video

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Nest.jsAuthenticationAPIJWTPassportNode.jsSecurityUser RegistrationLogin RouteToken Management