feat: Add resetPassword mutation

This commit is contained in:
Faruk AYDIN
2023-02-19 11:04:17 +01:00
parent 8ea176b5f0
commit 9dbfcf4262
4 changed files with 59 additions and 1 deletions

View File

@@ -87,6 +87,26 @@ class User extends Base {
await this.$query().patch({ resetPasswordToken, resetPasswordTokenSentAt });
}
async resetPassword(password: string) {
return await this.$query().patch({
resetPasswordToken: null,
resetPasswordTokenSentAt: null,
password,
});
}
async isResetPasswordTokenValid() {
if (!this.resetPasswordTokenSentAt) {
return false;
}
const sentAt = new Date(this.resetPasswordTokenSentAt);
const now = new Date();
const fourHoursInMilliseconds = 1000 * 60 * 60 * 4;
return now.getTime() - sentAt.getTime() < fourHoursInMilliseconds;
}
async generateHash() {
this.password = await bcrypt.hash(this.password, 10);
}