Skip to content

Commit 400790c

Browse files
committed
generate forgot password token
1 parent 7fb47df commit 400790c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Modals/userModels.js

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const moongoose = require('mongoose');
22
const validator = require('validator');
33
const bcrypt = require('bcryptjs');
44
const jwt = require('jsonwebtoken');
5+
const crypto = require('crypto');
56

67
const userSchema = new moongoose.Schema({
78
name: {
@@ -63,4 +64,23 @@ userSchema.methods.getJwtToken = function () {
6364
});
6465
};
6566

67+
// generate password reset token
68+
69+
userSchema.methods.getResetPasswordToken = function () {
70+
// generate token
71+
72+
const resetToken = crypto.randomBytes(20).toString('hex');
73+
74+
// Hash it and set to resetPasswordToken
75+
this.resetPasswordToken = crypto
76+
.createHash('sha256')
77+
.update(resetToken)
78+
.digest('hex');
79+
80+
// Set token Expire Time
81+
this.resetPasswordExpire = Date.now() + 30 * 60 * 1000;
82+
83+
return resetToken
84+
};
85+
6686
module.exports = moongoose.model('user', userSchema);

0 commit comments

Comments
 (0)