MEAN Stack Front To Back [Part 3] - User Model & Register
Skills:
Systems Design Basics90%Security Basics85%AI Systems Design80%Tool Use & Function Calling80%
Key Takeaways
The video demonstrates creating a Mongoose user model and schema, implementing user registration with password encryption using Bcrypt, and testing the registration system using Postman, all within the context of the MEAN stack and MongoDB database management.
Full Transcript
hey guys welcome to part three of the mean stack front to back Series in the last video we went ahead and set up our Express server and we connected to our mongodb database uh we also set up all the middleware for our dependencies for uh cores and body parser things like that and we also created our routes file so in routes users we have our register authenticate profile and validate now one thing that I forgot to do is register is actually a post route so we just want to change that to router. poost okay and we don't even need the validate I had that for some testing I was doing so we're going to get rid of that as well all right um that's the only thing that we need to change from what we've already done I believe we also set up our database config file um so now what we want to do is we want to create our model for our users okay because we're going to have a model that's going to hold all the Fe fields and field and um types of fields which will be name password email and username we're also going to have our functions that interact with the database in that file so let's go ahead and create a folder okay so we're going to create a folder called models and we're going to create a file in there called user.js okay so in here we want to bring in so we're going to set that to require and we also want to bring in um bcrypt for encryption and we want to set that to require and we're going to require bcrypt JS because that's what we're using and let's also bring in the config file so say cons config okay and we're going to go dot do slm models SL database okay so those are our includes uh now what we want to do is create a schema so this is going to be a user schema so we'll create a variable called user schema and set that to do schema okay that's going to take in some curly braces and then we Define our attributes okay so we want a name name and that's going to have type string next we'll have a let's see email type be string I actually want to set well name doesn't have to be required but email does so let's say required and we'll set that to True okay then we have username also a string and we'll also set that to True okay then password same thing okay so that that's our schema those are the attributes we want now we can always add more later so don't worry about that uh and then underneath that we want to create a variable called user uppercase U and we want to make it so that we can use this from outside so we need to do module exports and then we're going to set it to mongus do model which will take in the name of the model user and then the schema so user schema all right now I want to create two basic um functions in this file one to get the user by by their ID and want to get it by the username so when we want to use use functions outside we have to do module. exports and then do the function name so let's say get user by ID actually want to set that equal to function and that's going to take in the ID and call back and then down here we're just going to say user uh user dot we'll use the function find by ID and then that's going to take in ID and call back okay so now we can call this from outside we're also going to do by username okay that's going to take in username and then we have to create a query for this because we're going to use the find one function which takes a query so let's do cons query it's going to be an object we'll say username by username that's being passed in here okay and then we'll just change this to find one and change this to query okay and that should do it so let's save that so now we want to do excuse me we want to do our register so we're going to go to routes users and then to our post register again make sure this is post not get and actually let's bring in a couple things first so uh we're going to bring in our model so user dot Doods SL user and that's actually all we need right now but I might as well bring in the other stuff that we need so we'll bring in passport and we're also going to need the Json web token module so we'll say cons we'll call this JWT and set that to Json web token okay and then down here in the register we're going to create uh a new user so let's say const actually let's use let for this so let new user equals and then we're going to instantiate a user object from our model so new user and then we're going to pass in an object with a name and then we're going to get whatever is submitted in the form we can get with request. body. name email username and password all right and this is a plain text password we're going to run it through a function called Co um bt. has that'll hash it before it goes into the database so we have this user this new user object and now what we want to do with it is call a function called add user which we'll put in our model as well so let's do user. add user okay and then we're going to pass in here that new user object and then a call back okay and this call back is going to have an error if there is one and then user okay and then we're going to do our response so we'll say if there is an error then let's just do respond. Json I'm going to send some Json content and it's going to have a success value which is going to be false because the user isn't registered and then a message and the message will just say um failed to register user okay and then else so if everything goes okay we're going to again res. Json except we're going to say say success is true and our message will say user registered okay so let's save that and now let's create the add user inside of the model add user is a function okay and that's going to take in that new user object and then our call back uh uh call back okay because we we did the actual call back in the route and then in here uh let's see what we're going to do here we're going to go ahead and um hash the password now we need to call a function called bcrypt do gen salt so we're going to generate ass Sal which is basically like a random key that's used to Hash the the password password um we'll say it's going to be 10 um 10 the hell is it called let me just look this up real quick so bpjs you may want to look at the documentation anyways and let's see we need to do gen salt we're passing in 10 which is rounds 10 rounds and the default is 10 as well so we want to do that and then we want to have our call back which will have the error and salt and then we can call the hash function pass in the salt and it'll give us back our hash password all right so let's go ahead and do that so we'll put an arrow function here and that's going to be error and salt and then we want to do the hash so bcrypt do has it's going to take in the passwords so new user dot new user do password and then the salt and then the call back okay and that call back will give us an error if there is one and then the hash so now what we want to do is take the password that's in the new user object which is whatever they submit in the form and then uh make that into the hash so we can do that with new user do password equals hash okay and then we can go on to save it so new user. saave and just pass in that call back right there okay uh actually let's we didn't check for the error so we'll say if error throw error all right so we should now be able to register and also get our passwords encrypted so so let's test it out now I'm using Postman to do this if you want just um let see Postman Chrome and I'm using the Chrome extension so you can go ahead and download it it says launch app for me because I have it already installed but you can use anything that makes HTTP requests you could just use Curl if you want rest easy is another good utility um but we're just going to use Postman so let's make a post request to http Local Host 3000 SL users SL register and then we want to add for the headers we're going to add the content type I'm going to submit it as Json so application dojason and then the body I'm going to send raw and let's do uh actually let's do name John Doe email and username I'll just say John and password okay so we're submitting a plain text password but it's going to encrypt it for us so let's go ahead and try that could not get any response let see what this says uh required true unexpected identifier oh I didn't put a comma here and cannot find module models database oh that's wrong that's config database sorry about that guys hopefully that's it let's go ahead and try the register again send true user registered good so now if we go down here to the shell and say show DBS we have a mean off database so I'm going to say use mean off and let's say show collections so we have a users collection and look we didn't have to even set up this database we didn't have to create that collection um this is a lot more flexible than something like MySQL where you would have to go in and create your tables you'd have to create every single column you want along with each field type this is much more flexible so we can say db. users. find and then let's add pretty which will make it look a little cleaner and there's our gu so we have our ID which is an object ID kind of like a primary key in a relational database our name email username and look at the password the password is now encrypted okay so we're not storing those plain textt passwords all right so we now have a registration system so I'm going to cut it here and then in the next video we're going to set up a passport with a local Str not a local strategy a JWT strategy so that we can uh authenticate and get a token back okay so I'll see you in the next video
Original Description
In this video we will create our Mongoose user model and schema with the user fields. We will also complete the /register post route to sign up a new user and encrypt passwords with Bcrypt
Code Up To This Point:
http://www.traversymedia.com/downloads/meanauthapp/meanauthapp_part_3.zip
My Courses:
http://traversymedia.com/eduonix-courses
DONATIONS: Any donation is greatly appreciated....
https://www.paypal.me/traversymedia
https://www.patreon.com/traversymedia
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Traversy Media · Traversy Media · 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
Changing Your DNS/Nameservers
Traversy Media
Create a MySQL database in cPanel
Traversy Media
Install & Uninstall Joomla Extensions
Traversy Media
Adding and linking an article in Joomla
Traversy Media
Create a Joomla Blog
Traversy Media
Import & Export A MySQL Database
Traversy Media
Use A Custom Font On Your Website Using CSS
Traversy Media
Connect Joomla Site With Dreamweaver
Traversy Media
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
Add An Addon Domain In Cpanel
Traversy Media
Pull A Heroku Rails App and Database
Traversy Media
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
Font Dragr
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
JQuery FAQ Slider Tutorial
Traversy Media
301 Redirect With htaccess File
Traversy Media
Convert HTML to Wordpress Theme - Part 1
Traversy Media
Convert HTML to Wordpress Theme - Part 2
Traversy Media
Easy JQuery Widgets
Traversy Media
Codeigniter App Part 1 - Creating the Database
Traversy Media
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
Codeigniter App Part 6 - Login/Register System
Traversy Media
Codeigniter App Part 7 - Models List CRUD
Traversy Media
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
Node.js Part 3 - Building a Static Page Server
Traversy Media
Node.js Part 4 - NPM
Traversy Media
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
Install MongoDB in Ubuntu
Traversy Media
HTML5 Web Storage
Traversy Media
Create a Joomla Bootstrap Template From Scratch
Traversy Media
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
Create A Wordpress Widget - Part 1
Traversy Media
Create A Wordpress Widget - Part 2
Traversy Media
Create A Wordpress Widget - Part 3
Traversy Media
Create A Wordpress Widget - Part 4
Traversy Media
Get Started With Sass on Windows
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
Using Bootstrap With Ruby on Rails
Traversy Media
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Dev.to · Dipaditya Das
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
🎓
Tutor Explanation
DeepCamp AI