‫Welcome back.
‫So, authentication and authorization
‫is all about users signing up,
‫logging in, and accessing pages or routes,
‫that we grant them permission to do so.
‫So, it's really all about the users.
‫And, so we need to start by implementing the user model
‫in this lecture, so that in the next one,
‫we can then create new users in our database.
‫So, back in our application, let's go ahead
‫and create a new file for the user model.
‫So of course in our model folder, New File,
‫userModel.js.
‫And just like before, we start
‫by requiring the Mongoose package.
‫All right.
‫And now, all we need to do is to create a schema
‫and then create a model out of it.
‫So just like we did with the tours,
‫we're gonna do it here now with the users.
‫And actually, since we already did that before,
‫I want to leave this one as a challenge for you, all right?
‫So I want you to create a schema with five fields.
‫So, name, email,
‫then a photo; which is just gonna be a string,
‫so just like in our tours.
‫I also want a field for a password,
‫and for a passwordConfirm.
‫Okay?
‫And a bit more about that later
‫when I solve this challenge for you,
‫but for now, I really want you to practice
‫to create this schema on your own
‫and then also create a model out of that schema, okay?
‫And then by the end, you can also export it.
‫So please pause the video at this point,
‫and I'll be back in a second here
‫to solve this challenge for you.
‫All right.
‫Hope you completed that challenge.
‫And so I will now solve it.
‫And don't worry if it doesn't look exactly the same.
‫You can simply adapt it as we go through this video.
‫So, through this solution.
‫So, I'm gonna call this one the userSchema.
‫And just like before, we create a new mongoose.Schema.
‫And then into that, we pass our schema object,
‫and start defining our fields.
‫So, I want a name with the type, of course, of String.
‫And I also want it to be required.
‫So whenever a new user is created,
‫he needs to tell us his name basically.
‫So, let's set an error message here as well.
‫"Please tell us your name!"
‫All right?
‫So that's the name.
‫Next up, we want the email.
‫All right?
‫And so this will be used for the user to login
‫and we will not create a username property here, okay?
‫So in some applications, you will see a username being used,
‫but in this case, we wanna keep it simple here
‫and simply identify users by emails.
‫And so again, this is of the type String.
‫It is also a required field.
‫So just some error message again.
‫Next up, I want emails also to be unique, okay?
‫So, unique set to true.
‫And that's basically because this email is, of course,
‫the unique identifier of each user, okay?
‫Now by default, emails should be unique, right?
‫But let's just be 100% sure here
‫that there will be no accounts
‫with the same email address, all right?
‫Now we can also specify another schema-type option here,
‫which is lowercase.
‫So that's a new one.
‫And it's not a validator.
‫All it's gonna do, is to basically
‫transform the email into lowercase.
‫So if someone says like Jonas@Gmail or whatever,
‫then that's gonna be converted all to lowercase.
‫Okay?
‫And now finally, we also want to actually validate
‫the email address.
‫So basically testing if the provided email
‫that corresponds to the common email format, all right?
‫So for example again jonas@gmail.com
‫would be a valid email,
‫but for example, like this, it would not be.
‫Or like this.
‫And so for that, we need to create our own
‫validator basically.
‫Now we're not gonna create our own validator for that,
‫but instead we're gonna use that validator package
‫that I showed you before from npm, right?
‫So remember here in the tourModel,
‫I actually showed you this module here,
‫but then we didn't end up using it
‫because it wasn't really useful in our case.
‫But now, it's gonna be quite useful,
‫and so I'm going ahead and copying it here, okay,
‫so that we can now use it right in our schema.
‫So, to now create our custom validator,
‫we use validate, remember?
‫And then we pass in the function anti-error message,
‫just like with our other validators.
‫And to check if an email is valid,
‫all we need to do is to call the isEmail method
‫on the validator object, okay?
‫So that, again, comes from the documentation,
‫which I showed you before when we first used this module.
‫And so whenever you need a custom validator,
‫you can go ahead and check the documentation
‫and find if there's a good function for your use case.
‫So in this case, that's validator.Email,
‫and our error message is gonna be
‫"a valid email," okay?
‫And in the next video, when we're gonna create users
‫out of this model, we will then of course test all of this
‫in order to see if it works.
‫Next up, I also want the user to be able
‫to basically upload a photo.
‫And we're gonna store that simply in a String.
‫But it's not required, okay?
‫So a photo is usually optional in most web applications,
‫and so we're gonna do the same here.
‫So, if the user wants to upload a photo,
‫then that will be stored somewhere in our file system
‫and the path to that photo will then be stored
‫into this photo field, okay?
‫So that's similar to what we had here.
‫Remember, we have,
‫yeah, we have imageCover,
‫and so that also is simply the path
‫of the place in our file system
‫where the image is actually uploaded.
‫All right?
‫So that's photo.
‫And now we need password.
‫And the password should be, again, of the type String.
‫That's not correct.
‫And the password of course is required, right?
‫So, true.
‫"Provide a password."
‫And we're also gonna impose a very simple password rule,
‫which is that a password should have at least
‫eight characters.
‫So, how do we do that?
‫Well, that's quite easy.
‫All we have to do is minlength,
‫and set it to eight in this case.
‫And that's actually the only password rule
‫that we're gonna impose.
‫And many apps have all these crazy rules
‫like at least one number and one character and one symbol,
‫but we're not gonna implement any of that here
‫because actually it has been found
‫that that's not really effective, okay?
‫Usually the most secure passwords
‫are the longest ones, okay,
‫and not these ones with the crazy symbols and characters
‫and all that.
‫That's enough for the password for now,
‫but we're gonna create some more specific fields here
‫for the password when we start to like manage passwords
‫in the database, okay?
‫But for now, that is enough here.
‫And so now it's passwordConfirm.
‫So you know how usually when you create a new account
‫on some web application, you always need
‫to put in your password and then confirm it just to be sure
‫that they are the same.
‫And so that's what this field is for.
‫So of course it's also of type String.
‫And of course it's also required.
‫Okay.
‫And again, we're gonna create some more properties here
‫in the schema-type option a bit later, okay?
‫Specifically, validators to see if the confirm password
‫is actually the same than the main password, all right?
‫But for now, this schema is actually enough,
‫and so it has these fields that I asked you
‫to create in the challenge.
‫And now, actually, all we need to do is to go ahead
‫and create the model out of the schema.
‫So the User, with a capital U,
‫just to follow that convention that model variables
‫are usually always with a capital letter,
‫then mongoose.model, and then pass in the name of the model,
‫and of course, we want this one to be called User
‫and created out of the userSchema that we created before.
‫And then module.exports set to User.
‫And that's it.
‫That's our simple userSchema that's gonna allow us
‫to get started with actually creating users
‫right in the next video.
‫So, see you by then.