Building a CRUD REST API with Nest js, Prisma, and JWT token

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign back at lemonco 21 in this video we are making a simple JW key token API in sjs let's get started first let's create a new project by running the command nest's new project in the terminal [Music] then we'll clean up the project by removing three unnecessary files so that we can focus on the code development next we'll set up prettier a helpful tool for ensuring code consistency [Music] install the Prisma CLI as a development dependency now create your initial Prisma setup using the Prisma init command after that we'll navigate to the project folder and locate the schema.prisma file in this file We'll add the user table model to our Prisma schema this model will represent the user entity in our application we'll specify the attributes we want to include such as name email address password and more by adding this model we can easily access and manipulate user data in the database [Music] next we'll open the EnV file in our project and update the database connection settings according to our needs we'll adjust information such as the host Port database name username and password to connect our project to the appropriate database [Music] thank you foreign [Music] thank you [Music] thank you [Music] next we'll create a user's folder within our project inside this folder we'll add the user model using Prisma client this model will represent the data structure of a user including Fields like name email address and so on [Music] thank you next we'll create a service responsible for connecting our application to the database in this service we'll write the logic to fetch all user data from the users table in the database using Prisma client we'll leverage the methods provided by Prisma client to easily and efficiently retrieve data [Music] thank you [Music] thank you [Music] thank you [Music] thank you [Music] now we'll add the JWT library to our project this Library will facilitate the creation and validation of jwd tokens used for authentication purposes [Music] thank you [Music] we'll open the EnV file and add a secret key and token expiration time for JWT tokens the secret key will be used to encrypt and validate the tokens while the expiration time determines how long a token will be valid before it needs to be refreshed after that we'll create an authentication folder within our project inside this folder We'll add a jwt.strategy.ts file that will serve as the strategy for validating JWT tokens this file will implement the logic to verify the authenticity of tokens sent by users next [Music] foreign [Music] thank you [Music] thank you [Music] we'll create dto files which will be used to define the data structure sent during the ligand and register processes [Music] thank you foreign [Music] [Music] thank you foreign [Music] then we'll create an auth.service.ts file within the authentication folder this service will have functions for the login and register processes in the create user function We'll add logic to create a new user and save it to the users table using Prisma by integrating these modules we'll be able to handle user authentication with JWT tokens validate the tokens and securely store and retrieve user data from the database foreign [Music] thank you foreign [Music] foreign [Music] thank you foreign [Music] thank you [Music] thank you [Music] thank you [Music] foreign [Music] foreign [Music] thank you [Music] thank you [Music] thank you [Music] foreign [Music] thank you [Music] foreign [Music] this is [Music] thank you [Music] thank you [Music] thank you [Music] next we'll import the necessary modules for this integration we'll import the user module passport module and JWT module the user module is used to configure routes and logic related to users the passport module is used to integrate passport as middleware for Authentication and the JWT module is used to configure JWT settings then we'll add CRS configuration to our project [Music] foreign [Music] foreign [Music] thank you [Music] thank you [Music] thank you [Music] this configuration allows our application to accept requests from different Origins enabling us to access the API from a front end running on a different domain or port [Music] thank you thank you [Music] Let's test [Music] thank you [Music] next we'll add the JWT guard middleware to secure the API with a valid JWT token JWT guard will check the validity of the tokens sent by the user before granting access to protected roots guards are an sjs construct that determines whether a request should be allowed to proceed or not in this section you will Implement a custom JWT auth guard that will be used to protect routes that require Authentication [Music] thank you
Info
Channel: lemoncode21
Views: 6,774
Rating: undefined out of 5
Keywords: lemoncode21, backend enginner, frontend engineer, programming, javascript, typescript, nodejs, express, OOP, python, fastapi
Id: Mui0GNT7TrI
Channel Id: undefined
Length: 33min 10sec (1990 seconds)
Published: Tue Jun 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.