Chapter 6. Authentication
Because this application will be fully multi-user, the first gateway to build involves registration and identity authentication. Before users can access any other functionality, they must first identify themselves and prove they have authority to perform certain functions.
In this chapter you will create an account model to represent a user who has registered with your system, with the email address being the primary means of accessing the system. The user will also be expected to supply a password, which will be verified against the account with the matching email.
With a working account model, the next task will be creating login and registration views to bring users into and grant them access to the system.
Account
The account model is the main point of contact between Node.js and the MongoDB database.
The account model in Example 6-1 includes database
fields for an email address, password, name, photo, description, and
biography. This is a CommonJS module, which exports the account and
register, forgotPassword, changePassword, and login functions.
module.exports=function(config,mongoose,nodemailer){varcrypto=require('crypto');varAccountSchema=newmongoose.Schema({:{type:String,unique:true},password:{type:String},name:{first:{type:String},last:{type:String}},birthday:{day:{type:Number,min:1,max:31,required:false},month:{type:Number,min:1,max:12
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access