question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you detect if an attribute like password has changed

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I realise that this might be a noob question but i can’t find a reference in the docs. I am trying to a subscriber decorator to hash users password before persisting to the database.

In sequelizejs, I use the following code,

User.hashPassword = (user, options) => {
    if (!user.changed('password')) {
      return null;
    }
    // hash password
    return Bcrypt.hash(user.get('password'), SALT_ROUNDS)
      .then(hash => user.set('password', hash));
  };

Right now, my typeorm translation is roughly

@BeforeInsert()
@BeforeUpdate()
    hashPassword() {
        // conditional to detect if password has changed goes here
        this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
    }

Now, the issue is, I stuck at !user.changed('password'). Is there an equivalent function in typeorm to do this without rolling out my own solution?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:31 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
ssanusicommented, Nov 15, 2019

just to update this thread

public username: string;

@Column()
public password: string;

@Column({ nullable: true })
public jwtToken: string;

private tempPassword: string;

public toString(): string {
    return `${this.username}`;
}

@AfterLoad()
private loadTempPassword(): void {
    this.tempPassword = this.password;
}

@BeforeUpdate()
private encryptPassword(): void {
    if (this.tempPassword !== this.password) {
       this.password = await this.hashPassword(this.password);
        this.loadTempPassword()
    }
}
18reactions
Chrisigrimmcommented, Sep 8, 2018

you can make a something like this

@PrimaryColumn() public username: string;

@Column()
public password: string;

@Column({ nullable: true })
public jwtToken: string;

private tempPassword: string;

public toString(): string {
    return `${this.username}`;
}

@AfterLoad()
private loadTempPassword(): void {
    this.tempPassword = this.password;
}

@BeforeUpdate()
private encryptPassword(): void {
    if (this.tempPassword !== this.password) {
        //
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you detect if an attribute like password has changed ...
Show activity on this post. I just check if a password is present in the DTO (before update and insert). If it is...
Read more >
Check that a user has actually changed their password
To be able to determine if the password has actually been changed, we have to look at the meta data for the object...
Read more >
How to track object attribute changes - ManageEngine
Use Windows Event Viewer to track the attribute change​​ To view or access the event logs, open Event Viewer and click on Windows...
Read more >
Finding Last Password Changed for an Active Directory User ...
Navigate to the user account you want to know about using the standard OU structure, then right-click on the account and select “Properties”....
Read more >
How to Find Out Last Password Change in Active Directory ...
5. At 'User Properties' window, select the Attribute Editor tab. 6. Now scroll down to pwdLastSet attribute, to find out when a password...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found