- Advanced Node.js Development
- Andrew Mead
- 282字
- 2021-08-27 19:06:02
Creating a Mongoose user model for authentication
Now, we're going to create a brand-new Mongoose model. First up, you're going to make a new User model. Eventually, we're going to use this for authentication. It's going to store stuff like an email and a password, and the Todos are going to be associated with that User so when I create one, only I can edit it.
We'll look into all these, but for now, we're going to keep things really simple. On the User model, the only property that you need to set up is the email property. We'll set up others like password later, but it's going to be done a little differently since it needs to be secure. For now, we'll just stick with email. I want you to require it. I also want you to trim it, so if someone adds spaces before or after, those spaces go away. Last but not least, go ahead and set the type equal to a String, set type, and set minlength of 1. Now, obviously, you'll be able to pass in a string that's not an email. We'll explore custom validation a little later. This is going to let us validate that the email is an email, but for now this is going to get us on the right track.
Once you have your Mongoose model created, I want you to go ahead and try to create a new User. Create one without the email property, and then make one with the email property, making sure that when you run the script, the data shows up as expected over in Robomongo. This data should show up in the new Users collection.