Encryption and authentication

The encryption logic and salt generation logic, which are used to generate the hashed_password and salt values representing the password value, are defined as UserSchema methods.

mern-skeleton/server/models/user.model.js:

UserSchema.methods = {  authenticate: function(plainText) {    return this.encryptPassword(plainText) === this.hashed_password  },  encryptPassword: function(password) {    if (!password) return ''    try {      return crypto        .createHmac('sha1', this.salt)        .update(password)        .digest('hex')    } catch (err) {      return ''    }  },  makeSalt: function() {    return Math.round((new Date().valueOf() * Math.random())) + ''  }}

Additionally, the authenticate method is also defined as a UserSchema method, which is used when a user ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.