Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Web app with node express and vue 400x277
  • Overview
  • Transcript

3.1 Providing a Setup URL

Our application hinges upon a user, or at least a user that we can work with and it actually hinges on quite a bit. But we need a user in order to do just about everything. And in our case, we need to start off with an admin user. So we are going to set up an endpoint for creating an admin user, if one doesn't already exist. So let's create a new folder inside of server. This is going to be an endpoint with no UI, so we don't have to worry about doing anything with our client folder. And inside of server, we're going to create a folder called routes, because we're going to make sure that all of our code is separated into their appropriate concerns. Because in my opinion, it makes life a little bit easier on me. So inside of routes we're going to create a new file, and I'm going to call it basic. The idea being that these are just basic routes. Every application has these kinds of routes where you would have the home page or an about or a contact, or things like that. There's no authentication, there's nothing really extraordinary about it. It's just kind of the basic routes that every application has. So that's the idea here, I mean we could call them home routes or things like that but I think basic works. And we of course need a few things here. First of all is express, because we need to get a router from express. So let's go ahead and create that as well. We'll use express called the router function and that will give us a router. Now, we are going to encrypt the password for our users. So we are going to also need bcrypt. Now, what we're going to do is not what we would really want to do within an application and production. Because we are going to have the password in plain text inside of our code. Ideally we would not have that. And there's a variety of different strategies that we can use, and one is to just encrypt it beforehand and then have the encrypted version here within our code. I'm going to be lazy and we're going to do it incorrectly. But feel free to do it correctly if that's what you want to do. But definitely do it correctly in a production environment. Now, we do need our user model, so let's go ahead and pull that in. And we don't need the schema here, we just need that model class. So we will have our user and that's it for now. Now, a few lessons ago, we did create a route for our home page, just to prove that we had a working server application. Let's take that out. That's inside of main, and we're gonna put that inside of this basic file because I would consider this a basic route. And as far as the data that we send, let's have some kind of status, so we could say, Status: good Or something like that. And if we wanted to, we can add in some intelligence here to determine if our API is up and running or not. But [LAUGH] we can assume that if we see this status, then our application is working. If not, then there's definitely at least something wrong. So now let's just write our little endpoint for creating our admin user and let's just call this setup. And the idea is going to be very simple. We basically check to see if we have an admin user. And if we don't, we create it, otherwise we don't do anything. So the first thing we are going to do then is use the findOne method. And I'm going to see if we have a user with email address of admin@admin.com. Now, feel free to use whatever email address that you want to use. Whenever I just need an email address to represent a certain type of user, this is what I typically go for. I mean, yes, it's not going to work. But if I need something in order to get the application up and running, that's what I do. Later on, we're going to create a user@user.com to represent an individual regular user. So at least I'm consistent in being well, lazy. So our callback function. We'll have the error and then the document hopefully that we will retrieve. Now, if we don't have a user or rather if we do have a user, so we're going to check to see if it is not null, then we don't want to do anything. So we're just going to return something. And as far as the data, we can just pass in an empty object, that's fine. Otherwise, we will create our user. So let's go ahead and do that by newing up User. The email is going to be admin@admin.com. The password is going to be encrypted, and I'm going to use the super, super secure password of password. We're going to use hashSync function. We're going to hash password. And then we're going to set the is admin flag to true. Now, we're not going to do anything as far as the portfolio is concerned because we want to do all of that through the UI to make sure that everything is working there. So we're going to leave that alone for now. But we do want to save this, so let's save our user. And if we have an err, then we of course want to know about that err. So we will simply return something, and I guess we can just pass in the err object there. Otherwise, let's send a message to indicate that everything was okay. And we can just say success is true. That's good enough for me. And that's really all that we need for this particular endpoint. So now what we want to do is export our router so that we can then use it inside of our main.js file. So we'll have module.exports = router. And then we will import that here, and we'll call this basicRoutes. Just so that, as far as the code is concerned, we know without a doubt what this is. And this is inside of the not models, the routes folder and then basic. And then, all we have to do then is call the use method on our app pass in those routes. And, there we go, we should have that up and running. So we should be able to go to the browser. Let's refresh our homepage and something went horribly wrong. So we can see here that it cannot find the user model. So let's make sure that is the correct route. It's not, it's models. So let's go back, okay, that is working. So we can refresh the page here and the status is good. Let's go to our setup and we see that is success true. So that should have created our user. We can check our database here in a moment. And by refreshing the page we can see that nothing was done. So that is what I expected to see. So now inside of the database we want to go to our Collections. And then find our portfolio database and the users. So here, we will hopefully see that we have an admin user, we do. The password is of course, encrypted. Is admin is true. The portfolio has an array but no items in it. So we have our user. Our first step is done. In the next lesson we are going to start incorporating the UI, because now that we have a user, we need to be able to log in. And we will tackle that in the next lesson.

Back to the top