MERN Authentication Tutorial #2 - User Routes, Controller & Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right and gang so to begin with we're going to be focusing on the back end and more specifically we're going to make some user routes and controller functions and a user model as well to kind of control this whole authentication flow so at the minute the only set of routes we've got is for the workouts so we have a routes file for the workouts in the routes folder and those routes reference controller functions to handle the requests and those controller functions are inside the controller folder and they're the functions which do most of the heavy lifting when it comes to processing a request and sending a response to the browser and finally we register the workout routes in the server.js file by saying app.use and then using the workout routes for forward slash API forward slash workouts so we're going to follow the same process now for users so that we can handle sign up and login requests we're going to make a user's route file and a user controller as well and then we'll register those user routes in the server.js file so let's start by making a new file inside the routes folder called user.js for the user routes and first of all we need to require Express because we need that to make an instance of the express router to make these different authentication routes so we set that equal to a require and we want to require the Express package so let's grab that and then down here we need to say const router is equal to Express dot router with the capital r and involve that and that makes us an instance of the express router now at the end of the file we're going to be exporting this so let's say module exports is equal to that router so now what we need to do is attach different routes to this so we basically only need two things we need a login route so let's do a comment for that and also we need a sign up route so let's do a comment for that as well so the login route is going to be router dot post and they're both going to be post requests because we're going to be sending data to the server in each occasion so when we go to forward slash login for example we're going to be sending as the request body they're logging data their email and password and same goes for the sign up route as well so they're both going to be post request handlers and the path for this is going to be forward slash login like so and then right here this is where we would have our function our request Handler function now later on we're going to create these inside a user controller so I'm not going to fill this in for now I'll just leave it like that all right so now we can copy this and paste it down here and do the same thing but this time to forward slash sign up for the sign up ramp and that's pretty much it so far that's all we need to do inside the user routes file at the minute because we have no other routes that we need to handle at the minute so the next thing we want to do is create a user controller so let's create that new controller inside the controllers folder new file and we're going to call this user controller.js alright so inside here first of all we need a controller function for logging in a user so we'll say login use it right here and also one for signing up a user so we'll say sign up user right here all right so for this one we'll make a function called login user and we set that equal to an asynchronous function because later on we'll have asynchronous code inside this function to communicate with the database so inside this function as arguments we take in the request object and also the response object now we're not going to flesh out all of the logic inside these different controller functions at the minute I just want to set them up but what we will do is send back a response using the response object so response.json and we're going to send this object back with a message property and that's just going to say login user so if we send a request to forward slash login right here and it's a post request then it's going to fire eventually this function and we'll hook that up in a second and it's just going to send this response then so we'll do the same thing now down here for sign up user we'll call it sign up instead of login like so and right here we'll say sign up use it all right so that's pretty much all we need to do for now in fact we also need to export these so module dot exports and we set that equal to an object where we're going to have these two functions so inside there we'll say sign up user and login use it as well so we're exporting these two functions and now what we could do is import those inside this user routes file so that's what I'm going to do up here I'll do a little comment to say controller functions and then below that const and we want to destructure from that object that we export right here these two properties so I'll copy those like so and I'm going to paste them on here inside this object and then we set that equal to a require and we need to come out of the current controllers folder first of all so dot dot forward slash then we want to go oh sorry the correct routes folder that was then we want to go into the controllers folder then we want the user controller awesome so now we just need to hook them up right here so this first one was login user and then the second one was sign up user so we have these two request handlers set up now and they're hooked up to these two functions inside the user controller now we do also need to register these routes inside the server.js file so let's open up that server.js file and we need to require the workouts much like we did right here I'm just going to duplicate this one and I'll change workout routes over here to user routes and then also the file is called user okay so now we've imported them we also need to register them down here so again I'm going to copy this one but this time it's not going to be forward slash API forward slash workouts we go to it's forward slash API forward slash user and then forward slash whatever the route was here so log in and sign up okay and it's going to be the user routes that we want to use all right so that's all we need to do in terms of registering the routes right here so let's get rid of that and also get rid of this routes file there is one more thing I want to do and that is to create a user model just so we can use it later on so let's create that now user model dot JS inside the models folder and the model is going to be pretty simple it's just going to have an email and a password field so first of all we need to import Mongoose or require Mongoose so let's say const Mongoose is equal to require mongoose and then below that we need to create a schema so const schema is equal to mongoose dot schema like so and then we need to create the user schema so we'll say const user schema is equal to a new schema like so and we pass in an object to represent that schema with different properties we want an email property and then for that email property the type is going to be a string we also want it to be required so we'll set that to be true and then also we want this field to be unique because if someone signs up with a certain email address and if someone else tries to sign up with that email address we don't want those two accounts because they might get mixed up so by setting this to True over here what happens is if a user signs up with an email address and we save that to the database if someone tries to sign up again with the same email address if a record already exists in the database with that particular value for the email field then Mongoose won't allow us to save it so they all have to be unique all right so this time password it's the second property we want the type of this is going to be a string again and it's also required so we'll set that to be true it doesn't need to be unique people can have the same password if they wish all right so finally we just need to come down here and say module dot exports and we can set that equal to Mongoose dot model and then we're going to call this the user model and pass a schema which is the user schema so all we're doing here is defining a structure for the used documents when we save them to the database and Mongoose won't allow us to save documents to the database unless they adhere to this structure this schema all right so now we have that in place we're going to be using it inside the user controller so let's just import it at the top we'll say const user is equal to require and we need to come out of the controllers folder so let's say dot dot forward slash then into models and then we want the user model so we have this user model now ready to use down here and we'd use that to interact with the user's collection in our database to save new records or get records Etc technically they're called documents but I'm going to use records as the same thing so that is now the controller setup the route set up and also a model setup as well for the user what I want to quickly do is just try out these routes these two routes right here inside Postman just to make sure that everything is working all right then so I'm in Postman and by the way if you've never used Postman before it's just a tool that's completely free you can download it and it allows us to send requests to a back-end server just to test them out without having to build a front end so from the last series that I did all about the moon app we created these requests right here and I saved them what we're going to do is start making new ones for authentication now these are the workout ones so let's do a new one and it's going to be a post request and the address is going to be similar to this so let me grab that right there close it off and paste it in here this time it's going to be user and then forward slash login to begin with and then down here in the body we can add a body if we want to in fact it will just have a body raw and then Json at the minute we're not doing anything with that request body on the server but I'll set it up for later in case we need to use this request again so we'll say the email is going to be something like Yoshi at netninja dot Def and then also a password which is going to be ABC one two three all right so if we send this now hopefully we should get a response and it says login user awesome so that's working it's all hooked up correctly now what I'm going to do is save this request and it's going to go inside the moon app folder with the others and then I'm going to create a new one what I'll do is grab this URL and paste it in right here and this time it needs to be sign up let's make this a post request and also we need a body it's going to be raw and Jason and I'll just copy the body from over here wherever it is this stuff and paste in right here and that's it we just need to send fingers crossed yep and we get this response sign up user so that's all working awesome again I'm going to say this just so we can use it later but now my friends we have our user routes user controllers uh user model all set up so we can start flashing out those different controller functions using that user model and signing users up
Info
Channel: Net Ninja
Views: 45,611
Rating: undefined out of 5
Keywords: MERN, MERN stack, MERN tutorial, MERN auth, MERN authentication, MERN auth tutorial, MERN authentication tutorial, MERN stack tutorial, auth, auth tutorial, authentication tutorial, jwt, jwt tutorial, json web tokens, json web tokens tutorial, node auth, node auth tutorial, jwt auth, jwt auth tutorial, express auth, auth routes, user routes, authentication routes
Id: b5LDOW8WJ9A
Channel Id: undefined
Length: 12min 16sec (736 seconds)
Published: Tue Jul 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.