i'm learning Sequelize and i see something weird in the course,
const { Strategy } = require('passport-local');
const boom = require('@hapi/boom');
const bcrypt = require('bcrypt');
const UserService = require('./../../../services/user.service');
const service = new UserService();
const LocalStrategy = new Strategy({
usernameField: 'email',
passwordField: 'password'
},
async (email, password, done) => {
try {
const user = await service.findByEmail(email);
if (!user) {
done(boom.unauthorized(), false);
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
done(boom.unauthorized(), false);
}
delete user.dataValues.password;
done(null, user);
} catch (error) {
done(error, false);
}
}
);
module.exports = LocalStrategy;
My question is, when I want to compare passwords I use
bcrypt.compare(password, user.password);
and when I want to delete the password I use
delete user.dataValues.password
where does dataValues come from, by the way I'm using sequelize
I don't recommend to delete service props in Sequelize model (dataValues prop holds metadata about all fields of a certain model) because it can lead to incorrect work of this model.
Instead of deleting the password field form dataValues you need to delete it from a plain object that represents a certain model instance:
const { password, ...plainUser } = user.get({ plain: true })
done(null, plainUser);