April 2020
Intermediate to advanced
716 pages
18h 55m
English
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())) + '' }}
The UserSchema methods can be used to provide the following functionality: