@@ -29,18 +29,6 @@ export interface LoginAttributes {
29
29
password : string
30
30
}
31
31
32
- function setUserPassword ( instance : UserInstance ) {
33
- const { newPassword, confirmNewPassword } = instance
34
- const fdPassword = { newPassword, confirmNewPassword }
35
- const validPassword = schemaUser . createPassword . validateSyncAt (
36
- 'confirmNewPassword' ,
37
- fdPassword
38
- )
39
- const saltRounds = 10
40
- const hash = bcrypt . hashSync ( validPassword , saltRounds )
41
- instance . setDataValue ( 'password' , hash )
42
- }
43
-
44
32
interface UserCreationAttributes extends Optional < UserAttributes , 'id' > { }
45
33
46
34
interface UserInstance
@@ -61,17 +49,6 @@ const User = db.sequelize.define<UserInstance>(
61
49
} ,
62
50
} ,
63
51
{
64
- hooks : {
65
- beforeCreate ( instance : UserInstance ) {
66
- setUserPassword ( instance )
67
- } ,
68
- beforeUpdate ( instance : UserInstance ) {
69
- const { newPassword, confirmNewPassword } = instance
70
- if ( newPassword || confirmNewPassword ) {
71
- setUserPassword ( instance )
72
- }
73
- } ,
74
- } ,
75
52
defaultScope : {
76
53
attributes : {
77
54
exclude : [ 'password' , 'tokenVerify' ] ,
@@ -83,6 +60,26 @@ const User = db.sequelize.define<UserInstance>(
83
60
}
84
61
)
85
62
63
+ function setUserPassword ( instance : UserInstance ) {
64
+ const { newPassword, confirmNewPassword } = instance
65
+ const fdPassword = { newPassword, confirmNewPassword }
66
+ const validPassword = schemaUser . createPassword . validateSyncAt (
67
+ 'confirmNewPassword' ,
68
+ fdPassword
69
+ )
70
+ const saltRounds = 10
71
+ const hash = bcrypt . hashSync ( validPassword , saltRounds )
72
+ instance . setDataValue ( 'password' , hash )
73
+ }
74
+
75
+ User . addHook ( 'beforeCreate' , setUserPassword )
76
+ User . addHook ( 'beforeUpdate' , ( instance : UserInstance ) => {
77
+ const { newPassword, confirmNewPassword } = instance
78
+ if ( newPassword || confirmNewPassword ) {
79
+ setUserPassword ( instance )
80
+ }
81
+ } )
82
+
86
83
// Compare password
87
84
User . prototype . comparePassword = function ( candidatePassword : string ) {
88
85
return new Promise ( ( resolve , reject ) => {
0 commit comments