email verfication after account creation, password component, added password special characters
This commit is contained in:
@@ -72,6 +72,18 @@ const User = sequelize.define(
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
verificationToken: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
verificationTokenExpiry: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
verifiedAt: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true,
|
||||
},
|
||||
defaultAvailableAfter: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: "09:00",
|
||||
@@ -173,4 +185,41 @@ User.prototype.resetLoginAttempts = async function () {
|
||||
});
|
||||
};
|
||||
|
||||
// Email verification methods
|
||||
User.prototype.generateVerificationToken = async function () {
|
||||
const crypto = require("crypto");
|
||||
const token = crypto.randomBytes(32).toString("hex");
|
||||
const expiry = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
|
||||
|
||||
return this.update({
|
||||
verificationToken: token,
|
||||
verificationTokenExpiry: expiry,
|
||||
});
|
||||
};
|
||||
|
||||
User.prototype.isVerificationTokenValid = function (token) {
|
||||
if (!this.verificationToken || !this.verificationTokenExpiry) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.verificationToken !== token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (new Date() > new Date(this.verificationTokenExpiry)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
User.prototype.verifyEmail = async function () {
|
||||
return this.update({
|
||||
isVerified: true,
|
||||
verifiedAt: new Date(),
|
||||
verificationToken: null,
|
||||
verificationTokenExpiry: null,
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = User;
|
||||
|
||||
Reference in New Issue
Block a user