const crypto = require('crypto'); const uuid = require('uuid/v4'); const hash = Symbol(); const sha256 = text => crypto.createHash('sha256').update(text).digest('base64'); class User { constructor(email = '', password = '') { this._id = uuid(); this.email = email; this[hash] = sha256(password); } static fromDB(dbUser) { let user = new User(); user._id = dbUser._id; user.email = dbUser.email; user[hash] = dbUser.hash; return user; } toDB() { let user = new User(); user._id = this._id; user.email = this.email; user.hash = this[hash]; return user; } checkPassword(password) { return this[hash] == sha256(password); } } module.exports = User;