Node Auth Tutorial (JWT) #6 - Mongoose Hooks
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI