AutoCatBackend/models/user.js
2020-09-10 10:50:16 +03:00

36 lines
696 B
JavaScript

const crypto = require('crypto');
const { v4: uuidv4 } = require('uuid');
const hash = Symbol();
const sha256 = text => crypto.createHash('sha256').update(text).digest('base64');
class User {
constructor(email = '', password = '') {
this._id = uuidv4();
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;