-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get previous value in onSaving() #92
Comments
Hi @Aranir, As you mentioned, the To explain what I mean, let me go into the three different approaches you can take advantage of to deal with this. Option 1: Use Mutator MethodsThis is my personal approach when it comes to passwords which are hashed and salted, specifically because it prevents the password from being stored a defined variable in clear-text at any point. Granted, the password is still available in a closure throughout the execution of your methods, however it is far less likely to be accidentally leaked this way. interface UserDoc {
_id?: string;
email: string;
passwordHash: string;
}
class User extends Iridium.Instance<UserDoc, User> {
@Iridium.ObjectID
_id: string;
@Iridium.Property(String)
email: string;
@Iridium.Property(String)
passwordHash: string;
checkPassword(password) {
// Check whether the password matches the hash
return bcrypt.compareSync(password, this.passwordHash);
}
changePassword(newPass) {
this.passwordHash = bcrypt.hashSync(newPass, null);
}
} This approach, combined with some more restrictive validation rules on the Option 2: Rely on a TransformProperty transforms in Iridium are executed as part of the getter/setter on an object. This means that you can rather easily use them to perform conversion of the values automatically on assignment to the property. I tend to avoid this approach since it falls into the "magic" category and doesn't make it immediately obvious what is happening, but if you're after a solution which allows you to assign new passwords to the field and have them persisted as hashes, this might be your best option. interface UserDoc {
_id?: string;
email: string;
passwordHash: string;
}
class User extends Iridium.Instance<UserDoc, User> {
@Iridium.ObjectID
_id: string;
@Iridium.Property(String)
email: string;
@Iridium.Property(String)
@Iridium.Transform(value => value, value => bcrypt.hashSync(value, null))
passwordHash: string;
checkPassword(password) {
// Check whether the password matches the hash
return bcrypt.compareSync(password, this.passwordHash);
}
} This approach takes advantage of the fact that the transform will only be executed when you assign a new value, however you need to be careful that you don't Option 3: Use the onSaving hook and the
|
Thank you Benjamin, your answer was very helpful!. Yes i can see those are viable options. I still think that the original values of the entry being modified would be a helpful value to have for certain situations. Is there a specific reason why it would be harmful to have the previous value available in the |
Hi Aranir, The major issue is that, for consistency's sake, it would be necessary to implement a variant of the current Similarly, those internal APIs aren't guaranteed to remain the same - the moment we expose their data via external API endpoints we constrain how much flexibility we have to make changes to the underlying data structures (for performance, consistency or functionality reasons). I'll definitely keep your request in mind for future versions and see if I can find a nice way of handling your requirements though. |
The
onSaving()
method receives two objects:An
instance
and achangeSet
. The problem I have is that theinstance
already contains the modified values which will be applied through thechangeSet
.For only changing the (hashed and salted) passwords if they got modified, I would need to know the current (preSave) value of the (hashed and salted) password, compare it to the new password (from the change set) and then encrypt the new one if, and only if it got modified.
Currently I don't see a way how to retrieve the preSave value of the instance in the
onSaving()
function (as the_original
value is private).Any help would be appreciated.
The text was updated successfully, but these errors were encountered: