We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 95fd61b commit 1439504Copy full SHA for 1439504
src/models/User.ts
@@ -1,4 +1,5 @@
1
import { model, Schema, Document } from 'mongoose'
2
+import bcrypt from 'bcrypt'
3
4
export interface IUser extends Document {
5
displayName: string;
@@ -50,4 +51,14 @@ const UserSchema = new Schema({
50
51
timestamps: true
52
})
53
54
+UserSchema.pre<IUser>('save', async function(next){
55
+ const user = this
56
+ if (!user.isModified('password')) return next()
57
+
58
+ const salt = await bcrypt.genSalt(10)
59
+ const hash = await bcrypt.hash(user.password, salt)
60
+ user.password = hash
61
+ next()
62
+})
63
64
export default model<IUser>('User', UserSchema)
0 commit comments