So let's build a simple express app where we can implement some authentication using Decrypt.
So the principles you'll see here would apply to a beautiful, polished app where we have a lot more
features.
We're just going to make a very simple app where we have some protected route or maybe a couple of routes
you can't view unless you have logged in.
So there are quite a few pieces we need because we're going to need a database to store the username
and the hashed passwords.
We're going to need a what else we need decrypt, we need express.
I'm going to use edges because we need to make some templates with a form so that you can register and
log in.
So I'm going to init.
And then install Express Aegis Mongoose and bee crypt.
So anything else?
Let's see.
I might be forgetting something, but I think that's mainly all we need at the moment.
Okay, so while that's going well, that finished up, we make my index dogs.
I'll also make a views directory and a models directory.
We only have one model for this demo.
We need a user model.
So why don't we start there?
I'm going to make a new model here.
User dot js and a user simply has a username and a password and both are strings.
So I'm going to start by requiring mongoose.
Mongoose equals require mongoose and then we'll make our user schema equals new mongoose dot schema
and we want a username string and you definitely want to make sure that username and password are required
so we can do this here.
Username.
We'll do type string required.
True or we can do true here and provide some feedback.
Username cannot be blank.
Go away.
I hate that little window popping up.
Also you would want to make username unique, but I'll just keep it simple for now.
So then we'll have password.
Basically the same exact thing.
I'll just copy this.
Honestly.
We want it to be strong and we need it to be required.
We can't have an empty password.
All right, so that's our basic schema.
And then we will create our model, which some people just like to do as they export.
So that would be mongoose model, user, capital, U singular and then user schema.
Usually I like to do it on a separate line.
I think it's just cuter that way.
I don't know if I'm crazy, but it's just a matter of preference.
I like to have everything sort of done and then export it even though this is shorter.
I don't know.
I think I'm just weird about that, but I'll leave it.
That's fine.
So we have our basic model now.
Remember, we're not actually going to store the password here.
We're calling it password, but it's really the hashed password.
So if you prefer to do that and name it hashed password to help you remember, that's fine.
I mean, you can name it whatever you want.
I don't know why I would say it's fine.
Anything is fine.
It's up to you.
So now in our index JS, we'll set up our Basic Express app.
You know the drill require express connect app equals express.
Let's require our model while we're here.
So require and its dot slash model slash user.
And then we'll do an app.
Listen.
Obviously.
App.
Listen.
3000.
OC and console.log serving your app.
Now that we have that, we are going to write our first basic root, which is eventually going to be
secrets.
It's just going to be slash secret request response.
And for now, we'll do a resend.
This is secret.
Maybe we'll put a good secret there eventually.
But you cannot see me.
Unless you are logged in.
But that's not true at the moment.
I can definitely go to slash secrets, so let's just start up the server.
I always like to make sure things are working.
All right.
So far so good.
Let's go to localhost 3000 slash secrets.
Okay, that's working.
So our server is up and running.
So now we're going to implement the registration capability or functionality so that we can register.
You make a new user.
So to do that, we're going to need a couple of things.
First of all, a form.
So why don't we start there?
We'll just have a get slash register and this is just going to give you the form.
So that could also be user slash new or sign up or something.
And then in here it's going to be super simple.
We'll do a red dot render, which we don't have Edge's setup just yet, so we've got to do an app.
Dot what is it?
Set view engine to EJ's not eggs EJ's and I guess what is that other one?
I always forget these app set views and then I'm just going to set it to views.
I'm not going to bother doing the path thing just to keep it simple right now, even though we should
and then we'll render.
How about just register is the name of our templates.
So I've got to add that in here.
Register EJ's.
And let's just make a little template we won't bother with partials register put an h one in here sign
up.
Let's see if we can view that form.
We'll go to localhost 3000 slash register.
Good news.
There's the form.
So we're going to want to build the basic form out.
So we'll have a form and instead of here, we'll have an input type equals text, we'll give it the
name of username, we'll give it the ID of username so we can label it.
We'll also give it some placeholder of username.
We'll give it a label and the for is username, enter username or something like that.
Let's just put these in a little div together.
Why aren't you auto completing?
There we go.
Cut that.
Great.
And then let's just duplicate this whole thing for password.
So I'm going to replace password here with password.
I'm sorry, I'm going to replace username with password.
And then I'm going to change the type to be a password input.
So it just hides it with those little dots and then we'll add a button sign up.
Here it is.
Username, password.
So that's the basic form.
Now we need to figure out where we're going to submit our data to, and I think I'm going to just put
that in the next video just to break things up a bit so that it's not just one massive chunk.
So we've got the form, we're serving the form.
Nothing crazy, right?
No mongoose really at this point at all.
No script.
But that's about to change.
So that's coming up next.