Skip to content

Instantly share code, notes, and snippets.

@hypeJunction
Created March 22, 2019 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hypeJunction/01ad254fc2c06714293c276322c3f18e to your computer and use it in GitHub Desktop.
Save hypeJunction/01ad254fc2c06714293c276322c3f18e to your computer and use it in GitHub Desktop.
import BaseModel from '../db/BaseModel';
export default class User extends BaseModel {
static registerHooks () {
this.beforeSave((user) => {
if (user.changed('password') && user.password) {
if (user.password.substr(0, 7) === 'bcrypt$') {
throw new Error('Do not bcrypt passwords before setting them');
}
const hash = this.services.password.hash(user.password);
user.dataValues.password = `bcrypt$${hash}`;
}
});
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment