Node Auth Tutorial (JWT) #6 - Mongoose Hooks

Net Ninja · Beginner ·🔧 Backend Engineering ·5y ago

Key Takeaways

Uses Mongoose hooks in Node.js

Full Transcript

all right then gang so the next step in all of this is to hash a user's password when they sign up so that we're not storing their plain text password in the database like this but instead we're storing a hashed version of the password so that if the database is compromised in the future and the data exposed then the user's passwords will still be hashed and protected so to do this we'll be using a popular hashing package called be crypt and something called a mongoose hook in our model so in this lesson i just want to explain the second part the mongoose hook and then we'll look at bcrypt and actually hashing the password in the next video so a mongoose hook is a special function which fires after a certain mongoose event happens for example we could make a hook which fires a function after noon documents are saved to the database or deleted from the database all that kind of stuff okay so let's run through a couple of simple examples so first of all what i want to do is tell mongoose to fire a function after a new user has been saved to the database so to do that we're going to take the user schema and we're going to use a method called post now this does not refer to a post request it refers to something happening after something else has happened like post saving then fire a function so the first argument inside this function is the event that occurs so we're saying after a save event occurs then i want to fire a function so this function will fire whenever we've saved a new document to the database so inside here we get a couple of different arguments first of all the document which was saved and then also the next method now this is a bit like when we create custom middleware at the end of it we have to say next to go to the next middleware in the stack if we leave this out then the code is just going to get hungrier because it won't move on and we probably won't get a response and i'll demo that in a minute but first of all what i'm going to do is just log to the console here so console.log and we'll say new user was created and saved and then over here i'm going to output the dock as well so let me save this i'm not firing the next function but if we come over here we need to change oops we need postman we need to change this because it's not unique anymore so i'm going to change this to toad at google.com and i'm going to try to sign up now at the minute we're not getting any kind of response and that's because like i said we're not using the next method now if we open up the console we can still see this new user was created and the user so this down here is all working we're just hanging here then we're not doing anything else we're not moving on because we're not calling the next method we always have to do that at the end of any kind of mongoose middleware or hook so let us now call the next method and by the way you can still see that hanging right here but if we save this now and then come over here and try to send this again in fact we can't do this because this will still probably have been saved to the database so let me check that by let's just minimize this and go to the database and refresh we're probably going to see that that email has now been taken up um yes we can see toad at google.com so even though we didn't get a response we still saved it because this happens after the save event right it's just that we're not firing next and we're not sending that response then to the client so it's still saved and we need a new email now for this to work so let me say instead of toad we'll go with another mario character peach all right so send this and hopefully yep we now get a response because we called next over here all right so this is firing after the documents have been saved to the database so that's one way of working what if we want to do something before something happens so what if in my case i wanted to fire a function before a document was saved well let me do a comment first of all fire a function before doc saved to db and then i'm gonna say user schema and this time instead of post we need to use a different hook pre so that just means before right so before saving then we're going to fire a function and by the way i'm using a normal function here not an arrow function because i want to use the this keyword to refer to the instance of the user that we're trying to create so this inside this function refers to the user instance you know like in app.js we create or rather not app.js what we're talking about inside the auth controller we create this thing right here this instance basically that creates a new instance of a user object locally and then saves it behind the scene we get access to that instance of the object locally here before it's saved to the database and that's what this refers to right here if we used an arrow function instead of this then we won't have the value available to us okay so we don't get the document inside here because it's not yet been saved to the database this is running before we save it to the database all we get inside here now is the next method which we still need to call at the end okay now before we do that i'm gonna log something to the console i'll say console.log and we'll say user about to be created and saved and then we'll output this which refers to the local instance of the user before we saved it to the database so let me save that now and let's try another character yoshi and send that request now this all works it's fired back to that user for us and if we take a look over here in the console we should see two things user about to be created and saved and the local version of the user which is this and then new user was created and saved after we've saved it to the database with that user right here and notice we don't have this up here because we only get this property after the database creates that user so you can also fire functions either before or after other events too for example you could replace save with something like remove now if you want to learn more about the different things you can do with hooks definitely check out the docs i'll leave the link to that down below and now we know about these hooks in the next video we're going to use this pre-hook right here to hash passwords before we save users to the database so when we save that user the password will be hashed and not the plain text password they provide us with

Original Description

In this Node auth tutorial we'll take a look at how mongoose hooks can be used to fire code at different points when documents are saved to the database. Mongoose middleware - https://mongoosejs.com/docs/middleware.html 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/node-express-jwt-auth 🐱‍💻 🐱‍💻 Other Related Free Courses & Links: + Node.js Cash Course - https://www.youtube.com/watch?v=zb3Qk8SG5Ms&list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU + Get VS Code - https://code.visualstudio.com/ + JWT Debugger - https://jwt.io/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
This Cop Was Held Accountable For His Brutality! #police #lawyer
Hampton Law
Watch →