fix: password cannot be empty (#3491)
This commit is contained in:
		
							parent
							
								
									c0988d9fc6
								
							
						
					
					
						commit
						9ff785dca4
					
				@ -19,7 +19,7 @@ describe('password field', () => {
 | 
				
			|||||||
      fields: [{ type: 'password', name: 'password' }],
 | 
					      fields: [{ type: 'password', name: 'password' }],
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    await db.sync();
 | 
					    await db.sync();
 | 
				
			||||||
    const user = await User.model.create<any>({
 | 
					    let user = await User.model.create<any>({
 | 
				
			||||||
      password: '123456',
 | 
					      password: '123456',
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    const pwd = User.getField<PasswordField>('password');
 | 
					    const pwd = User.getField<PasswordField>('password');
 | 
				
			||||||
@ -27,5 +27,9 @@ describe('password field', () => {
 | 
				
			|||||||
    user.set('password', '654321');
 | 
					    user.set('password', '654321');
 | 
				
			||||||
    await user.save();
 | 
					    await user.save();
 | 
				
			||||||
    expect(await pwd.verify('654321', user.password)).toBeTruthy();
 | 
					    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();
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
import crypto from 'crypto';
 | 
					import crypto from 'crypto';
 | 
				
			||||||
import { DataTypes } from 'sequelize';
 | 
					import { DataTypes } from 'sequelize';
 | 
				
			||||||
 | 
					import { Model } from '../model';
 | 
				
			||||||
import { BaseColumnFieldOptions, Field } from './field';
 | 
					import { BaseColumnFieldOptions, Field } from './field';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface PasswordFieldOptions extends BaseColumnFieldOptions {
 | 
					export interface PasswordFieldOptions extends BaseColumnFieldOptions {
 | 
				
			||||||
@ -46,7 +47,7 @@ export class PasswordField extends Field {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  init() {
 | 
					  init() {
 | 
				
			||||||
    const { name } = this.options;
 | 
					    const { name } = this.options;
 | 
				
			||||||
    this.listener = async (model) => {
 | 
					    this.listener = async (model: Model) => {
 | 
				
			||||||
      if (!model.changed(name as any)) {
 | 
					      if (!model.changed(name as any)) {
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -55,7 +56,7 @@ export class PasswordField extends Field {
 | 
				
			|||||||
        const hash = await this.hash(value);
 | 
					        const hash = await this.hash(value);
 | 
				
			||||||
        model.set(name, hash);
 | 
					        model.set(name, hash);
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        model.set(name, null);
 | 
					        model.set(name, model.previous(name));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user