Node js Authentication Sytem with OTP and Express js | Restful Api-Bcrypt-JWT-Axios | Cabbage-2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to caves after long times i come with new tutorial today i'll show a back and field i show you something new idea aor system by using node express and i am building a restful api in this tutorial i built a restful api with otp that means one time password i use node express and some npm package to build it so the video is going to be very interesting if you are not subscribed our channel yet just subscribe it and hit the bell icon to never miss our new idea let's dive in the video [Music] here we create a folder named odd system i write code on this folder let's open the folder we have to initialize this folder as an npm package open cmd in this folder type npm init and hyphen y for passing some basic questions hit the enter button it initialized this folder as npm package successfully we have to open this folder in code editor here we can see the package.json in package.json file we have to write one scripts as a use not want restarting the server in the scripts type start nodemon server.js now we have to install some npm package for that go to cmd here type npm i xjs decrypt dot env express json web token low dash mongoose otp generator hit the enter button here we can see the npm installing the packages [Music] okay npm successfully install this package here in the package.json we can see all the dependencies are listed now create one file after js here we have to write some code first import the dependencies const express require express const app express app.use express.json now we have to export it module.exports app [Music] we have to create a server and connect to mongodb database create one new file server.js before writing code in server.js we have to create one more file.env in the env file we keep all sql information like api url port and zwt secret key etc type in capital letter mongodb url local it will be tv slash localhost colon 27017 slash auth system port will be 3001 jwt secret key will be some text go to cyber.js and type require dot env slash config const mongos require mongos const app require app then create a mongodb connection [Music] type mongoose.connect process.env here copy the mongodb local url from env file and paste it then we have to allow some parser use new url parser to use unified topology true use create index true then we catch the result dot then a callback function console.log connected to tv or catch a callback error console.log mongodb connection field successfully created the connection with mongodb now we have to create a server type const port process dot env port or 3001. app.listen port callback function console.log f running on port dynamic portion port [Music] okay now check the server is running successfully here we can see the server is running but i misspell it here it will be 2 now it's looking good now we have to create an all system that's why we have to create three folders in this tutorial we actually following the mvc architecture create open folder named model then create router one more folder named controllers in the model folder create file user model.js here type const curly braces require mongos in the curly process import schema and model cons jwt require json web token [Music] const user schema schema number type string required true here we create our user schema at first we want to get just number for giving otp one time password timestamps true creating time and date now we have to create a token to give the user we use jwt here's joison web token user schema that methods dot generate jwt function [Music] const token tursin id this underscore id number this number process dot env copy jwd secret key from the env file and paste it here we can give the expiration time expires in seven day then we have to create a model i export it also modules.export.users model user user schema here we successfully create the user model we have to create a new model for restoring otp that is otp model create file otpmodel.js type const curly process require mongos in the car differences import schema model here we export it module.exports.otp model otp schema [Music] number type string require true [Music] otp type string required true now we have to delete this data after some time that is otp expirations here we want to express an otp five minutes after five minutes this otp deleted automatically for that type create a date type date default date dot now index expires 300 seconds [Music] it means after five minutes it deleted automatically from the database give the timestamps time stamps true [Music] now we have to create usercontroller.js const big crypt require be crypt const underscore require loaders const axios require axios const otp generator require otp generator i have to import user and otp model as well const user require dot dot slash model slash user model const otp require dot slash model slash otp model [Music] here i am create two function one for getting number and sending otp and another for verifying the otp module dot exports dot sign up async rec res callback function [Music] one more module dot exports verify otp async rank or s call back function then create userrouter.js for routing the request const router require express dot router const calibrate controllers slash user controller router dot route slash sign up this is post request dot post sign up router dot route slash sign up slash verify this is also post request first import verify otp and insert here now export this module dot exports router now go to app.js and define the router first import the router from user router const user router require dot slash routers slash user router then define the post request url app.use slash api slash user user router now we have to write the function go to usercontroller.js file here at the first function const user await user dot find one number as this is reg dot body dot number if i find an user then return raise dot status 400 percent user already registered [Music] then generate the otp one time password const otp otp generator dot generate we want six digits length otp [Music] digits will be true alphabets will be false upper case will be false and this also will be false [Music] const number reg.body.number to see the otp console.log otp create the collection const otp new otp number will be number otp will be otp now we have to sort the otp otp.otp await [Music] otp bigcrypt.hash.otp.org otp salt type before const salt await big crypt or gen salt 10 digits hash key now we can save it to our database const result await otp dot save return raise dot status 200 dot sent otp sent successfully this function is created successfully now we have to check it go to the postman ios postman for retesting the result here the request url localhost 3001 slash api slash user slash sign up this will be post request i send a raw data from body then type kali phrases number something number send the request here we can see one error i think we mistake somewhere let's investigate it here in app.js everything look good and user router.js i think i missed it here i have to give one slash before the sign of text now let's check again it's successfully run here we can see otps and successful messages in the shell here also save the otp and number we make it here in the otp we can see the otp save as a hashed key now nobody can understand the otp it is being more secure now in the console we can see the otp as we print in the console now we have to write another functions for verifying the otp and also for saving users type const otp holder avoid otp dot find number as this is reg.body.number if there is no tp in the database return raise dot status 400 percent you use an expired otp now one user can request more time to send the otp that's why we find multiple otp as bases on number but we have to check very first otp for that type const write otp find otp holder otp holder dot length -1 it catch the first object from an array now compare it with our database otp const valid user await bigcrypt dot compare otp file from request body compare with write otp find dot otp now we have to check the number as well as valid user type if right otp find dot number will be react.body.number and and valid user [Music] const user new user from loaders function dot underscore.peak reactor body here we pick only number create the token const token user dot generate jwt from user model.js const result await user.save when it is assigned as an user the otp data is stored in our database it should be delete for deleting the otp const otp delete avoid otp dot delete many number as basis right otp find dot number [Music] now we can return it return raise dot status 200 send message user registration successful token will go as token data will go as result [Music] now write else value for this if statement is we return an error message return raise dot status 400 dot sent your otp was wrong now let's check it at first create the otp otp sent successfully now check in database we can see it now one more post request to sign up and verify the body we send raw data select.json type numbers something number otp first i test by giving and wrong otp [Music] message we can see your otp was wrong now give the original otp and send we can see user registration successful but here we can see the token i think i mistake somewhere let's see it in user model.js file here i have to return the token okay fine now i have to implement sms gateway for sending sms with otp here i use one local sms gateway for sending the sms you can use any sms gateway in the sms schedule read the documentation from documentation i copy the user's code and let's paste it [Music] token can find from here the sms gateway number will be number and the messes contain the otp [Music] send request for otp here in mobile screen i show you we can see the message come successfully with one term password let's verify it give otp here and send it [Music] we see successful message great we successfully create the our system manually you can use it for your back-end app thanks for watching subscribe our channel and hit the bell icon to never miss our new idea [Music] you
Info
Channel: Cabbage Tube
Views: 35,265
Rating: undefined out of 5
Keywords: Node js, express js, rest api, restful api, mongoDB, auth system, aythentication system, otp, one time password, auth system with otp, auth system with onetime password, authentication system node js, auth system with restful api, auth system with express js, auth or authentication system with otp or onetime password using node express and restful api, api node js, axios node js, async function node js, bcrypt node js, fetch node js, authentication express js, express js api
Id: f6oyA6kkt1w
Channel Id: undefined
Length: 19min 46sec (1186 seconds)
Published: Fri Sep 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.