MEAN Stack Front To Back [Part 3] - User Model & Register

Traversy Media · Intermediate ·⚡ Algorithms & Data Structures ·9y ago

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 Changing Your DNS/Nameservers
Changing Your DNS/Nameservers
Traversy Media
2 Create a MySQL database in cPanel
Create a MySQL database in cPanel
Traversy Media
3 Install & Uninstall Joomla Extensions
Install & Uninstall Joomla Extensions
Traversy Media
4 Adding and linking an article in Joomla
Adding and linking an article in Joomla
Traversy Media
5 Create a Joomla Blog
Create a Joomla Blog
Traversy Media
6 Import & Export A MySQL Database
Import & Export A MySQL Database
Traversy Media
7 Use A Custom Font On Your Website Using CSS
Use A Custom Font On Your Website Using CSS
Traversy Media
8 Connect Joomla Site With Dreamweaver
Connect Joomla Site With Dreamweaver
Traversy Media
9 Remove Phoca Gallery 3.2.3 Footer Text
Remove Phoca Gallery 3.2.3 Footer Text
Traversy Media
10 Drupal 7 Security Update 7.19 to 7.20
Drupal 7 Security Update 7.19 to 7.20
Traversy Media
11 Add An Addon Domain In Cpanel
Add An Addon Domain In Cpanel
Traversy Media
12 Pull A Heroku Rails App and Database
Pull A Heroku Rails App and Database
Traversy Media
13 Create a Custom Joomla 2.5 Module - Part 1
Create a Custom Joomla 2.5 Module - Part 1
Traversy Media
14 Create a Custom Joomla 2.5 Module - Part 2
Create a Custom Joomla 2.5 Module - Part 2
Traversy Media
15 Create a Custom Joomla 2.5 Module - Part 3
Create a Custom Joomla 2.5 Module - Part 3
Traversy Media
16 Joomla SEO Tutorial - sh404sef Configuration
Joomla SEO Tutorial - sh404sef Configuration
Traversy Media
17 Font Dragr
Font Dragr
Traversy Media
18 Convert an HTML Template to Joomla 2.5/3.0 - Part One
Convert an HTML Template to Joomla 2.5/3.0 - Part One
Traversy Media
19 Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Convert an HTML Template to Joomla 2.5/3.0 - Part Two
Traversy Media
20 Rockettheme Rocketlauncher   Joomla Site in Under 10 Minutes
Rockettheme Rocketlauncher Joomla Site in Under 10 Minutes
Traversy Media
21 JQuery FAQ Slider Tutorial
JQuery FAQ Slider Tutorial
Traversy Media
22 301 Redirect With htaccess File
301 Redirect With htaccess File
Traversy Media
23 Convert HTML to Wordpress Theme - Part 1
Convert HTML to Wordpress Theme - Part 1
Traversy Media
24 Convert HTML to Wordpress Theme - Part 2
Convert HTML to Wordpress Theme - Part 2
Traversy Media
25 Easy JQuery Widgets
Easy JQuery Widgets
Traversy Media
26 Codeigniter App Part 1 - Creating the Database
Codeigniter App Part 1 - Creating the Database
Traversy Media
27 Codeigniter App Part 2 - Installation and Configuration
Codeigniter App Part 2 - Installation and Configuration
Traversy Media
28 Codeigniter App Part 6 - Login/Register System
Codeigniter App Part 6 - Login/Register System
Traversy Media
29 Codeigniter App Part 7 - Models List CRUD
Codeigniter App Part 7 - Models List CRUD
Traversy Media
30 Codeigniter App Part 8 - Models Task CRUD
Codeigniter App Part 8 - Models Task CRUD
Traversy Media
31 Node.js Part 1 - Install NodeJS on Windows
Node.js Part 1 - Install NodeJS on Windows
Traversy Media
32 Node.js Part 3 - Building a Static Page Server
Node.js Part 3 - Building a Static Page Server
Traversy Media
33 Node.js Part 4 - NPM
Node.js Part 4 - NPM
Traversy Media
34 Node.js Part 2 - Install MongoDB in Windows
Node.js Part 2 - Install MongoDB in Windows
Traversy Media
35 Create a Joomla Quickstart with Custom Sample Data
Create a Joomla Quickstart with Custom Sample Data
Traversy Media
36 Install MongoDB in Ubuntu
Install MongoDB in Ubuntu
Traversy Media
37 HTML5 Web Storage
HTML5 Web Storage
Traversy Media
38 Create a Joomla Bootstrap Template From Scratch
Create a Joomla Bootstrap Template From Scratch
Traversy Media
39 Ubuntu Server 14.04 Setup Part 1 - Installation
Ubuntu Server 14.04 Setup Part 1 - Installation
Traversy Media
40 Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Ubuntu Server 14.04 Setup Part 3 - Set Static IP
Traversy Media
41 Create A Wordpress Widget - Part 1
Create A Wordpress Widget - Part 1
Traversy Media
42 Create A Wordpress Widget - Part 2
Create A Wordpress Widget - Part 2
Traversy Media
43 Create A Wordpress Widget - Part 3
Create A Wordpress Widget - Part 3
Traversy Media
44 Create A Wordpress Widget - Part 4
Create A Wordpress Widget - Part 4
Traversy Media
45 Get Started With Sass on Windows
Get Started With Sass on Windows
Traversy Media
46 Build An HTML5 Template With Bootstrap and SASS - Part 1
Build An HTML5 Template With Bootstrap and SASS - Part 1
Traversy Media
47 Build An HTML5 Template With Bootstrap and SASS - Part 6
Build An HTML5 Template With Bootstrap and SASS - Part 6
Traversy Media
48 Build An HTML5 Template With Bootstrap and SASS - Part 4
Build An HTML5 Template With Bootstrap and SASS - Part 4
Traversy Media
49 Build An HTML5 Template With Bootstrap and SASS - Part 5
Build An HTML5 Template With Bootstrap and SASS - Part 5
Traversy Media
50 Build An HTML5 Template With Bootstrap and SASS - Part 3
Build An HTML5 Template With Bootstrap and SASS - Part 3
Traversy Media
51 Build An HTML5 Template With Bootstrap and SASS - Part 2
Build An HTML5 Template With Bootstrap and SASS - Part 2
Traversy Media
52 Build An HTML5 Template With Bootstrap and SASS - Part 7
Build An HTML5 Template With Bootstrap and SASS - Part 7
Traversy Media
53 Build An HTML5 Template With Bootstrap and SASS - Part 10
Build An HTML5 Template With Bootstrap and SASS - Part 10
Traversy Media
54 Build An HTML5 Template With Bootstrap and SASS - Part 8
Build An HTML5 Template With Bootstrap and SASS - Part 8
Traversy Media
55 Build An HTML5 Template With Bootstrap and SASS - Part 11
Build An HTML5 Template With Bootstrap and SASS - Part 11
Traversy Media
56 Build An HTML5 Template With Bootstrap and SASS - Part 9
Build An HTML5 Template With Bootstrap and SASS - Part 9
Traversy Media
57 Build An Audio Player Using HTML5 & jQuery - Part 1
Build An Audio Player Using HTML5 & jQuery - Part 1
Traversy Media
58 Build An Audio Player Using HTML5 & jQuery - Part 2
Build An Audio Player Using HTML5 & jQuery - Part 2
Traversy Media
59 Youtube Data API v3 & jQuery To List Channel Videos
Youtube Data API v3 & jQuery To List Channel Videos
Traversy Media
60 Using Bootstrap With Ruby on Rails
Using Bootstrap With Ruby on Rails
Traversy Media

This video teaches how to create a user model and implement user registration with password encryption in a MEAN stack application, using Mongoose, Passport, and Bcrypt. It also covers testing the registration system using Postman and MongoDB for database management.

Key Takeaways
  1. Create a user model with attributes name, email, username, and password
  2. Implement functions to get a user by ID and username
  3. Use Passport for authentication
  4. Register user with POST request
  5. Hash the password using Bcrypt
  6. Save the user to the database
  7. Test registration system using Postman
💡 Using Bcrypt to encrypt user passwords and implementing token-based authentication with JWT provides an additional layer of security for user data.

Related Reads

📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Learn to optimize palindrome detection using Manacher's Algorithm with mirror boundary optimization, reducing time complexity to O(N)
Dev.to · Dipaditya Das
📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Solve the Product of Array Except Self problem on LeetCode to improve coding skills and learn array manipulation techniques
Medium · AI
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →