Building Node.js Authentication from Scratch

Поділитися
Вставка
  • Опубліковано 27 січ 2023
  • ► Master NodeJS Playlist: • Master NodeJS
    ► Source Code: github.com/piyushgargdev-01/s...
    In this tutorial video, you will learn how to create a secure and robust authentication system for your Node.js application from scratch. We will cover the basics of user authentication and authorization and show you how to implement login and registration functionality using Node.js and MongoDB.
    ► Complete Full Stack Web Developer RoadMap 2023: • Complete Full Stack We...
    Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript on the server side, creating server-side applications with JavaScript.
    ► My Website: www.piyushgarg.dev
    My Gears
    ► My Girlfriend: amzn.to/3WD6FRp
    ► Apple MacBook Laptop: amzn.to/3WBJgQn
    ► Anker USB Hub: amzn.to/3GhZSr0
    ► Blue Yeti Microphone: amzn.to/3YKZ9FT
    ► External 27” Monitor: amzn.to/3Vp3xaO
    ► Logitech MK295 Wireless Keyboard and Mouse: amzn.to/3DuL1bB
    ► Seagate Expansion 1TB External HDD: amzn.to/3QMm5Q8
    ► Tripod: amzn.to/3S4OwK4
    ► Ring Light: amzn.to/3YLf8DR
    Disclaimer: All the links above are affiliate links.
    Social Links
    ► Twitter - / piyushgarg_dev
    ► LinkedIn - / piyushgarg195
    Video Titles
    Securing Your Node.js App with User Authentication
    Authentication 101: Implementing Login in Node.js
    Adding User Login to Your Node.js Application
    Authentication in NodeJS
    Tags
    #nodejs #authentication #javascript #expressjs #mongodb #mernstack #developer #api #https #server #javascriptinhindi #webdevelopment #webapp #realtimeapp #serverside #nonblockingio #tech

КОМЕНТАРІ • 104

  • @iamakashkumarram
    @iamakashkumarram Рік тому +31

    This is the best NodeJS series ever made.

  • @kanikamehrotra1518
    @kanikamehrotra1518 10 місяців тому +1

    Thankyou so much for the wonderful playlist.

  • @AkshaySharma-bg3oj
    @AkshaySharma-bg3oj 6 місяців тому +19

    Thanks a lot for availing this series for free, we know it takes hell amount of your time.
    Thanks mate.

  • @varmapurushotham
    @varmapurushotham Місяць тому +1

    Enjoyed the entire video! Lot of learning! Thank you :)

  • @ankit1153
    @ankit1153 11 місяців тому +1

    Thank you sir i don't know about how to find data in other collection by user ID but after watching this video i am clear now...

  • @iamakashkumarram
    @iamakashkumarram Рік тому +18

    Sir please cover JWT Authentication and Local Storage in depth.

  • @VarunSharma-xd8xd
    @VarunSharma-xd8xd Місяць тому +1

    when i saw this playlist for first time i was frustrated but now i am able to grasp the concepts

  • @iUmerFarooq
    @iUmerFarooq Рік тому +3

    Bohot acha sa samjatha ho.
    Make more projects in Nodejs/Expressjs.
    Bohot Shukriya

  • @KrishnaKumar-kh5iu
    @KrishnaKumar-kh5iu 4 місяці тому +14

    🎯 Key Takeaways for quick navigation:
    00:00 🛠 *Node.js Authentication Basics*
    - Introduction to authentication patterns in Node.js.
    - Two basic patterns discussed: Stateful and Stateless authentication.
    01:07 🚗 *Authentication Analogy: Parking Ticket*
    - Illustration of stateful authentication using a parking attendant analogy.
    - Comparison between providing a parking ticket and receiving a unique ID for server-side data maintenance.
    02:02 🎟 *Stateless Authentication: Diary Entry*
    - Explanation of stateless authentication using the example of a parking ticket without server-side data storage.
    - Analogous process of generating and validating unique IDs for accessing resources.
    03:38 🔄 *Transition to Server-Side Authentication*
    - Transition from client-side to server-side authentication process.
    - Example of a client sending a request with user credentials to the server for authentication.
    04:09 📝 *Server-side session management*
    - Understanding session IDs and their management on the server-side,
    - Sending session IDs along with requests for server authentication,
    - Utilizing cookies or headers for transferring unique IDs and managing authentication.
    06:02 🛠 *Implementing authentication in the application*
    - Setting up authentication middleware in the Express application,
    - Validating tokens and unique IDs for authentication,
    - Incorporating user data and authentication parameters into the application model.
    08:52 🛠 *Creating Authentication Routes*
    - Setting up authentication routes in Node.js.
    - Creating a user model and defining routes for signup.
    - Implementing controller functions for user signup.
    10:13 📝 *User Signup Validation*
    - Validating user input such as password length and email format.
    - Handling user signup requests and responses.
    - Importing user model and returning appropriate responses.
    11:50 📲 *Handling User Registration*
    - Implementing registration functionality for users.
    - Defining routes and handling user registration requests.
    - Creating user signup forms with input validation.
    14:17 🛠 *Handling user sign-up form submission*
    - Understanding form action and method,
    - Troubleshooting form submission errors and refreshing the page.
    16:04 🔑 *Ensuring successful user registration process*
    - Configuring form redirection after successful registration,
    - Implementing conditional logic for user access based on authentication status.
    17:11 🚦 *Validating user credentials and redirecting*
    - Validating user input for email and password,
    - Implementing error handling and redirecting based on authentication status.
    20:07 🛠 *Node.js Authentication Setup*
    - Setting up authentication process in Node.js,
    - Generating unique session IDs,
    - Installing necessary packages and importing them.
    21:32 🍪 *Cookie Generation and Handling*
    - Creating and handling cookies for user sessions,
    - Utilizing middleware to retrieve cookie values,
    - Implementing functionality to refresh cookies upon server request.
    22:59 📝 *Application Diary and Control Flow*
    - Demonstrating control flow within the application,
    - Logging user activities in the application diary,
    - Managing user sessions and associated data.
    24:56 🔒 *Middleware Function for User Authentication*
    - Implementing middleware function for user authentication,
    - Accessing user ID from cookies,
    - Handling redirection based on user authentication status.
    27:55 🍪 *Cookie Parsing and Validation*
    - Parsing and validating cookies for user authentication,
    - Ensuring proper access control based on URL paths,
    - Handling cookie-related errors gracefully.
    29:02 🛠 *Troubleshooting and Debugging*
    - Debugging cookie reading and generation issues,
    - Resolving errors related to undefined properties,
    - Iterative debugging process for smoother development.
    30:27 🛠 *Node.js Authentication Implementation*
    - Implementing password generation and authentication in Node.js.
    - Discussion on server restart issues affecting data integrity.
    - Handling URL generation and user identification securely.
    31:48 🔄 *URL Generation and Data Integrity*
    - Addressing data integrity challenges upon server refresh.
    - Ensuring correct URL generation and user identification after server restart.
    - Implementing strategies to maintain data consistency and user authentication.
    33:19 🖥 *Dynamic URL Handling and User Identification*
    - Exploring middleware for dynamic URL handling and user identification.
    - Adding user-specific references to generated URLs.
    - Utilizing user IDs for personalized URL management and authentication.
    34:44 🔒 *Implementing authentication logic*
    - Creating middleware function to check authentication status.
    - Simplifying authentication logic by restricting access.
    - Handling authentication checks and redirects efficiently.
    36:07 🧳 *Setting up user authentication*
    - Creating a sign-up form with fields for full name, email, and password.
    - Generating and validating short IDs for user authentication.
    - Implementing basic authentication workflow and URL handling.
    37:57 🚪 *Customizing user access and permissions*
    - Limiting user access to their own analytics and URLs.
    - Generating short IDs for user-specific data access.
    - Ensuring secure and controlled authentication processes.

    • @pranjalmaurya3832
      @pranjalmaurya3832 4 місяці тому

      hey! do you faced the same problem at 29:00 i.e. not being able to generate short urls after login?

    • @Solo_playz
      @Solo_playz 2 місяці тому +2

      Bhai video se jyada heavy Tera comment hai ☠️

    • @whyDude123
      @whyDude123 2 місяці тому

      @@Solo_playz Ai se generated hai

    • @IamMQaisar
      @IamMQaisar Місяць тому

      @@whyDude123 🤣

  • @imPrathamDev
    @imPrathamDev Рік тому +22

    First time I see Gravity Falls and Adventure Time crossover

  • @ratishjain2718
    @ratishjain2718 6 місяців тому +9

    Best Nodejs series out there and I am not even kidding how good he teaches

    • @rishabhraj8233
      @rishabhraj8233 6 місяців тому

      yes man @piyush Garg is a god sent man

    • @mujibulhaquetanim
      @mujibulhaquetanim 5 місяців тому

      But don't think that's a full series on Node.js; he covered it yesterday in his live stream.

    • @Ayush37262
      @Ayush37262 5 місяців тому

      ​@@mujibulhaquetanim Complete nhi hai kya ye??

    • @mujibulhaquetanim
      @mujibulhaquetanim 5 місяців тому

      This playlist is still suitable for beginners, even though there are still some gaps in the graphql series. He nearly covered every essential topic related to Node.js.@@Ayush37262

  • @mma-dost
    @mma-dost Рік тому +2

    Great video bhaiya !! Please make more videos asap

  • @arbabmemon2095
    @arbabmemon2095 8 місяців тому

    You have been doing great job sir

  • @ASHISHKUMAR-de9jn
    @ASHISHKUMAR-de9jn Рік тому +1

    Thank you sir ❤

  • @polymath-403
    @polymath-403 Рік тому

    Thanks Buddy

  • @easelogic
    @easelogic 6 днів тому +1

    Best Series. 👩🏻‍💻👩🏻‍💻

  • @inlifegadgets
    @inlifegadgets Рік тому

    Great Video Thanks Sir

  • @webdeveloper3529
    @webdeveloper3529 Рік тому +2

    Wow i cleared my all concepts 😎

  • @gamingwithasmile268
    @gamingwithasmile268 Рік тому

    Thanks sir❤

  • @pabloescobar.exe.
    @pabloescobar.exe. 10 місяців тому

    Hey piyush, can u make videos on styling web pages using tailwind css Or just css, in UI

  • @rohitghadage3220
    @rohitghadage3220 11 місяців тому

    Sir setting user details in cookies would be done with express-session by default right?

  • @SulavGhimireeee
    @SulavGhimireeee 6 місяців тому

    Thanks bro

  • @sahildhanawade
    @sahildhanawade 10 місяців тому +1

    my doubt
    apne sessionIdToUserMap object me user ki uid or user ki details store ki hai pr ek time ke baad ye server ki bhot memory kha jayega to es object me se hr user ki details ek time ke baad apne aap delete ho jana chahiye

  • @coolcoder-bh9ic
    @coolcoder-bh9ic 8 місяців тому

    SIr on deployment why user persistence not there ?
    means after one time logged in in any device i can generate url in another device also without logged in ?

  • @fun3.1
    @fun3.1 18 днів тому

    nicely explain

  • @MetroExodus999
    @MetroExodus999 Місяць тому

    good job bro

  • @ShivendraPratap524
    @ShivendraPratap524 Рік тому +3

    sir thanks for the awesome content ,
    but i have a request that please try to explain things ,
    for ex : cookies , etc
    please , it would help us understand more easily

    • @piyushgargdev
      @piyushgargdev  Рік тому +1

      Noted

    • @shivanshsrivastava5561
      @shivanshsrivastava5561 4 місяці тому

      ​@@piyushgargdevafter loggin with email and password we are redirected to page to generate shortUrl or shortId but when I enter original url after loggin I am being redirected back to loggin page ,,unable to resolve

    • @sarfaraz_adil
      @sarfaraz_adil 2 дні тому

      @@shivanshsrivastava5561 hii same problem bro can you please help me

  • @indrajitnaskar6851
    @indrajitnaskar6851 4 місяці тому +2

    I clone your code ,but the urls that user visited is not working, both users data is showing, please some one help me

  • @sparsh-0384
    @sparsh-0384 7 місяців тому

    Well explained... Where I can get the code for reference

  • @thebishalpaul
    @thebishalpaul 11 місяців тому +5

    Understood everything except checkAuth at 35:22 tried using restrictToLoggedInOnly middleware instead of checkAuth the home page redirects to login page but it doesn't load. Can anyone plz help why is it so?

    • @shubham0546
      @shubham0546 5 місяців тому

      Same issue

    • @Ayush37262
      @Ayush37262 5 місяців тому

      What do you mean by "it doesn't load"?? Can you explain in detail...

  • @thebishalpaul
    @thebishalpaul 11 місяців тому +10

    Bhaiya can you plz describe why are we again getting user and checking if user exists (line 8 at 29:14) as, if the user isn't logged in then the UID won't be generated so the first check serves the purpose isn't it?
    Edit: Found out that on refresh the map obj doesn't reset but on server restart it does. For which the second check is important.
    Keeping the comment might help someone with same doubt.

    • @ombandurkar7950
      @ombandurkar7950 7 місяців тому +2

      Thank you for keeping the comment, it helped me

    • @thebishalpaul
      @thebishalpaul 7 місяців тому

      @@ombandurkar7950 happy to help 🙂

    • @sarfaraz_adil
      @sarfaraz_adil 2 дні тому

      @@thebishalpaul after loggin with email and password we are redirected to page to generate shortUrl or shortId but when I enter original url after loggin I am being redirected back to loggin page ,,unable to resolve can you help me please

  • @ibntofajjal
    @ibntofajjal Рік тому +2

    Damn Bro!

  • @SUMITDAS-tf7jh
    @SUMITDAS-tf7jh 11 місяців тому

    bhaiya email authentication ka bhi ek video plz bana dena with forget fassword 😊😊

  • @lovelymusic3549
    @lovelymusic3549 Рік тому +1

    Sir when react JS course is coming any idea sir?

  • @fearwayesloot
    @fearwayesloot 4 місяці тому

    Bhaiya if middle man proxy use karega he will get our api how we secure

  • @lucifer.morningstar9523
    @lucifer.morningstar9523 3 місяці тому

    What if we want to pass the data to frontend like react app instead of .ejs views files
    How to do it ?

  • @PubG-dl5eh
    @PubG-dl5eh Рік тому +3

    Osm series💥
    But can you add the complete authentication like....
    Sign up
    Otp verify
    Email verify
    Login
    ......etc

  • @vikashgupta3305
    @vikashgupta3305 3 місяці тому +1

    just WOOWWWWW

  • @sheikhumaid9213
    @sheikhumaid9213 2 місяці тому

    Is this process production ready?

  • @dethaliyayashhimatbhai6170
    @dethaliyayashhimatbhai6170 14 годин тому

    Bro he already explained in his previous videos

  • @UserNotFound-py5eg
    @UserNotFound-py5eg 8 місяців тому

    Can you make a proper video on 'How to create a chrome extension'
    Please

  • @AtulKumar-pi2ky
    @AtulKumar-pi2ky Рік тому

    Please create a video on google-reCaptcha v2 verification in a registration form to avoid scripting much need and it is not available on any channel where an API is made for that please please make it if possible

  • @AbhishekRaut0001
    @AbhishekRaut0001 Місяць тому

    Key Takeaways for quick navigation:
    00:00 Node.js Authentication Basics
    - Introduction to authentication patterns in Node.js.
    - Two basic patterns discussed: Stateful and Stateless authentication.
    01:07 Authentication Analogy: Parking Ticket
    - Illustration of stateful authentication using a parking attendant analogy.
    - Comparison between providing a parking ticket and receiving a unique ID for server-side data maintenance.
    02:02 Stateless Authentication: Diary Entry
    - Explanation of stateless authentication using the example of a parking ticket without server-side data storage.
    - Analogous process of generating and validating unique IDs for accessing resources.
    03:38 Transition to Server-Side Authentication
    - Transition from client-side to server-side authentication process.
    - Example of a client sending a request with user credentials to the server for authentication.
    04:09 Server-side session management
    - Understanding session IDs and their management on the server-side,
    - Sending session IDs along with requests for server authentication,
    - Utilizing cookies or headers for transferring unique IDs and managing authentication.
    06:02 Implementing authentication in the application
    - Setting up authentication middleware in the Express application,
    - Validating tokens and unique IDs for authentication,
    - Incorporating user data and authentication parameters into the application model.
    08:52 Creating Authentication Routes
    - Setting up authentication routes in Node.js.
    - Creating a user model and defining routes for signup.
    - Implementing controller functions for user signup.
    10:13 User Signup Validation
    - Validating user input such as password length and email format.
    - Handling user signup requests and responses.
    - Importing user model and returning appropriate responses.
    11:50 Handling User Registration
    - Implementing registration functionality for users.
    - Defining routes and handling user registration requests.
    - Creating user signup forms with input validation.
    14:17 Handling user sign-up form submission
    - Understanding form action and method,
    - Troubleshooting form submission errors and refreshing the page.
    16:04 Ensuring successful user registration process
    - Configuring form redirection after successful registration,
    - Implementing conditional logic for user access based on authentication status.
    17:11 Validating user credentials and redirecting
    - Validating user input for email and password,
    - Implementing error handling and redirecting based on authentication status.
    20:07 Node.js Authentication Setup
    - Setting up authentication process in Node.js,
    - Generating unique session IDs,
    - Installing necessary packages and importing them.
    21:32 Cookie Generation and Handling
    - Creating and handling cookies for user sessions,
    - Utilizing middleware to retrieve cookie values,
    - Implementing functionality to refresh cookies upon server request.
    22:59 Application Diary and Control Flow
    - Demonstrating control flow within the application,
    - Logging user activities in the application diary,
    - Managing user sessions and associated data.
    24:56 Middleware Function for User Authentication
    - Implementing middleware function for user authentication,
    - Accessing user ID from cookies,
    - Handling redirection based on user authentication status.
    27:55 Cookie Parsing and Validation
    - Parsing and validating cookies for user authentication,
    - Ensuring proper access control based on URL paths,
    - Handling cookie-related errors gracefully.
    29:02 Troubleshooting and Debugging
    - Debugging cookie reading and generation issues,
    - Resolving errors related to undefined properties,
    - Iterative debugging process for smoother development.
    30:27 Node.js Authentication Implementation
    - Implementing password generation and authentication in Node.js.
    - Discussion on server restart issues affecting data integrity.
    - Handling URL generation and user identification securely.
    31:48 URL Generation and Data Integrity
    - Addressing data integrity challenges upon server refresh.
    - Ensuring correct URL generation and user identification after server restart.
    - Implementing strategies to maintain data consistency and user authentication.
    33:19 Dynamic URL Handling and User Identification
    - Exploring middleware for dynamic URL handling and user identification.
    - Adding user-specific references to generated URLs.
    - Utilizing user IDs for personalized URL management and authentication.
    34:44 Implementing authentication logic
    - Creating middleware function to check authentication status.
    - Simplifying authentication logic by restricting access.
    - Handling authentication checks and redirects efficiently.
    36:07 Setting up user authentication
    - Creating a sign-up form with fields for full name, email, and password.
    - Generating and validating short IDs for user authentication.
    - Implementing basic authentication workflow and URL handling.
    37:57 Customizing user access and permissions
    - Limiting user access to their own analytics and URLs.
    - Generating short IDs for user-specific data access.
    - Ensuring secure and controlled authentication processes.

  • @Akshay-be3xt
    @Akshay-be3xt 2 місяці тому

    can someone please share me code ...i have write but getting error...

  • @bm9code
    @bm9code 6 місяців тому

    how setUser is able to get the value stored getUser even after reloding page

    • @bm9code
      @bm9code 6 місяців тому

      solution will be given in next video 38:45

  • @samad8906
    @samad8906 3 місяці тому

    It would have been better if u would use postman or any server client

  • @amitkumarlkr
    @amitkumarlkr 4 місяці тому +1

    Done, Done, Done, Done..... 😂😂😂

  • @zeta_meow_meow
    @zeta_meow_meow Рік тому

    github code is not working
    home.ejs file mai (urls) giving error

    • @saubhagyalive3061
      @saubhagyalive3061 11 місяців тому

      same man !!
      did u find the solution ?
      also the redirectURL in index is also giving error to me

  • @ajiteshmishra0005
    @ajiteshmishra0005 2 місяці тому

    Is this called JWT authentication??

  • @pranjalmaurya3832
    @pranjalmaurya3832 4 місяці тому

    After login same login page is rendering similar to that what was happening in your code(30:00), what can be done?

    • @unknown-vx6ei
      @unknown-vx6ei 4 місяці тому

      Check if you have written method and action in your login ejs file 🤔

    • @myselfashutosh
      @myselfashutosh 3 місяці тому

      is your error solved coz im aslo getting same

    • @theiitjourneytij3377
      @theiitjourneytij3377 3 місяці тому

      @@myselfashutosh @unknown-vx6ei @pranjalmaurya3832 maybe in your code you are doing req.cookie.uid do one task do console.log(req) now you will se a last item cookie:"uid=something" but when you use cookieparser it will store your cookie in cookies so when you print the req object you will see two different keys in request object one is cookie and one is cookies:{uid:something} now in cookies you can access req.cookies.uid as in cookies uid is a key name of the inner object but in cookie it is uid="something" which means even if you use req.cookie?.uid then it will always return undefined as it doesnt have uid as key

    • @saiteja_13
      @saiteja_13 Місяць тому

      same issue, anyone plz explain

  • @pallabdandapat1866
    @pallabdandapat1866 Місяць тому

    27:30 --> cookie start

  • @NeerajSharma1
    @NeerajSharma1 8 місяців тому

    Object Id create karte time apne ref: 'users' liya hai user ki jagah. Phr bhi ye work kar rha h. Why ?

    • @sanketmane4026
      @sanketmane4026 5 місяців тому +1

      automatically mongodb me collections vaise hi create hota hai ...
      for example: const URL = mongoose.model("url", urlSchema);
      => show collections => urls
      for example: const User = mongoose.model("user", userSchema);
      ==> show collections => users
      model me tum koi aur name dalo aur mongosh me jake show collection karoge to tumhe dikhega

  • @souravkumar961
    @souravkumar961 Місяць тому

    hmm, It seems like you merge two lecture in one video(authentication & cookie). for the first time, cookie implementation is much difficult then authentication.

  • @rishabhkedia9304
    @rishabhkedia9304 12 днів тому

    7:10
    16:20 login
    20:33 cookie
    31:00

  • @bm9code
    @bm9code 6 місяців тому

    zip file of knowledge

  • @sanchitasethi9885
    @sanchitasethi9885 7 місяців тому

    My girlfriend 🤣🤣🤣🤣🤣
    What an Easter egg hidden here haahahahaha

  • @cihadp
    @cihadp 8 місяців тому

    What language do you speak?

  • @namanjain1684
    @namanjain1684 19 днів тому +1

    why you don't explain anything like why you are using controller and what is that and other stuffs

    • @Chhotu668
      @Chhotu668 10 годин тому

      He already explained in his previous videos in the playlist.

  • @gtbaba123
    @gtbaba123 2 місяці тому

    Bro use postman so that u don't have to waste time on creating FE as this video is totally focused on Node JS

    • @pranaavbv9359
      @pranaavbv9359 2 місяці тому

      I mean, he's also giving us an idea about SSR 🤷🏿‍♂️

  • @buland_aavaj1130
    @buland_aavaj1130 2 місяці тому

    bhai yrr ejs mat use krna kro na pls real world me kya use h iska

  • @indrajitnaskar6851
    @indrajitnaskar6851 4 місяці тому +1

    Sir, please first try once before video, instead it's so confusing

  • @joelosei765
    @joelosei765 9 місяців тому +1

    If you make this video in English it's going to be great,

  • @kumaranupam8301
    @kumaranupam8301 Місяць тому

    bkl

  • @sameerprajapati9467
    @sameerprajapati9467 Місяць тому

    bhut he ghatiya padhaya hai

  • @user-be3pb2sf5s
    @user-be3pb2sf5s 5 місяців тому

    if your video is not in english? so why are you writing the title in english??!! take my downvote

    • @gamerschoice779
      @gamerschoice779 2 місяці тому

      Learn Hindi

    • @sourikray2291
      @sourikray2291 2 місяці тому +1

      well...... there are lots of people like me over India who can understand hindi but can't read it properly so for me it's fine as it is

  • @prakharsinha4145
    @prakharsinha4145 11 місяців тому +1

    req.user._id undefined .... anybody with the isssue ?

    • @myselfashutosh
      @myselfashutosh 3 місяці тому

      yep bro same issue how did you solved

    • @036kaushik5
      @036kaushik5 24 дні тому

      user User.findOne({}) instead of User.find(), as the first method returns an object the latter returns an array of objects

    • @Ronakrewar
      @Ronakrewar 14 днів тому

      @@036kaushik5 req.user._id undefined .... anybody with the isssue ? how you resolved it