fix: password cannot be empty (#3491)

This commit is contained in:
chenos 2024-02-05 22:56:36 +08:00 committed by GitHub
parent c0988d9fc6
commit 9ff785dca4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View File

@ -19,7 +19,7 @@ describe('password field', () => {
fields: [{ type: 'password', name: 'password' }],
});
await db.sync();
const user = await User.model.create<any>({
let user = await User.model.create<any>({
password: '123456',
});
const pwd = User.getField<PasswordField>('password');
@ -27,5 +27,9 @@ describe('password field', () => {
user.set('password', '654321');
await user.save();
expect(await pwd.verify('654321', user.password)).toBeTruthy();
user.set('password', null);
await user.save();
user = await User.model.findOne();
expect(await pwd.verify('654321', user.password)).toBeTruthy();
});
});

View File

@ -1,5 +1,6 @@
import crypto from 'crypto';
import { DataTypes } from 'sequelize';
import { Model } from '../model';
import { BaseColumnFieldOptions, Field } from './field';
export interface PasswordFieldOptions extends BaseColumnFieldOptions {
@ -46,7 +47,7 @@ export class PasswordField extends Field {
init() {
const { name } = this.options;
this.listener = async (model) => {
this.listener = async (model: Model) => {
if (!model.changed(name as any)) {
return;
}
@ -55,7 +56,7 @@ export class PasswordField extends Field {
const hash = await this.hash(value);
model.set(name, hash);
} else {
model.set(name, null);
model.set(name, model.previous(name));
}
};
}