From 9ff785dca447a4da39e4e1c5822d19c691d113df Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 5 Feb 2024 22:56:36 +0800 Subject: [PATCH] fix: password cannot be empty (#3491) --- .../database/src/__tests__/fields/password-field.test.ts | 6 +++++- packages/core/database/src/fields/password-field.ts | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/core/database/src/__tests__/fields/password-field.test.ts b/packages/core/database/src/__tests__/fields/password-field.test.ts index 263e0081a..6f6a09388 100644 --- a/packages/core/database/src/__tests__/fields/password-field.test.ts +++ b/packages/core/database/src/__tests__/fields/password-field.test.ts @@ -19,7 +19,7 @@ describe('password field', () => { fields: [{ type: 'password', name: 'password' }], }); await db.sync(); - const user = await User.model.create({ + let user = await User.model.create({ password: '123456', }); const pwd = User.getField('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(); }); }); diff --git a/packages/core/database/src/fields/password-field.ts b/packages/core/database/src/fields/password-field.ts index dfd6e2d21..32848b135 100644 --- a/packages/core/database/src/fields/password-field.ts +++ b/packages/core/database/src/fields/password-field.ts @@ -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)); } }; }