Richiesta Cambio Password: ti manda il codice a 6 cifre e poterlo inserire sulla APP.

This commit is contained in:
Surya Paolo
2022-12-11 18:04:02 +01:00
parent 3eef70f3f3
commit 1d7a98fc3f
4 changed files with 102 additions and 37 deletions

View File

@@ -139,6 +139,10 @@ const UserSchema = new mongoose.Schema({
tokenforgot_code: {
type: String,
},
retry_pwd: {
type: Number,
default: 0,
},
date_tokenreg: {
type: Date,
},
@@ -1178,29 +1182,42 @@ UserSchema.statics.findByLinkTokenforgotCode = function (idapp, email, tokenforg
});
};
UserSchema.statics.createNewRequestPwd = function (idapp, email) {
UserSchema.statics.createNewRequestPwd = function (idapp, email, code) {
const User = this;
const sendemail = require('../sendemail');
return User.findByEmail(idapp, email).then(async (user) => {
if (!user) {
return false;
} else {
// Creo il tokenforgot
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).
toString();
user.date_tokenforgot = new Date();
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
user.lasttimeonline = new Date();
return await user.save().then(async () => {
await sendemail.sendEmail_RequestNewPassword(user.lang, user, user.email, user.idapp, user.tokenforgot, user.tokenforgot_code);
return true;
if (code && code.length === 6) {
return User.findByLinkTokenforgotCode(idapp, email, code)
.then((user) => {
if (user)
return { ris: true, link: tools.getlinkRelativeRequestNewPassword(idapp, email, user.tokenforgot) };
else
return { ris: false };
}).catch((e) => {
console.log(' Err createNewRequestPwd', e.message);
res.status(400).send();
});
}
} else {
return User.findByEmail(idapp, email).then(async (user) => {
if (!user) {
return { ris: false };
} else {
// Creo il tokenforgot
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).
toString();
user.date_tokenforgot = new Date();
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
user.lasttimeonline = new Date();
return await user.save().then(async () => {
await sendemail.sendEmail_RequestNewPassword(user.lang, user, user.email, user.idapp, user.tokenforgot, user.tokenforgot_code);
});
return { ris: true };
});
}
});
}
};
UserSchema.statics.createNewRequestPwdByUsernameAndGetLink = async function (idapp, username) {
@@ -1436,7 +1453,7 @@ UserSchema.statics.getUserById = function (idapp, id) {
UserSchema.statics.getUserByUsername = function (idapp, username) {
const User = this;
return User.findOne({
return User.findne({
idapp,
username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
@@ -3671,14 +3688,14 @@ UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
},
},
{
$match: { "count": { $gte: 2 } }
$match: { "count": { $gte: 2 } }
},
{
$sort: {
count: -1,
},
},
{ $limit: 20 },
{ $limit: 20 },
{
$lookup: {
from: "users",
@@ -3715,11 +3732,11 @@ UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
{
$replaceRoot: {
newRoot: {
$mergeObjects: [ "$user", "$$ROOT" ],
$mergeObjects: ["$user", "$$ROOT"],
},
},
},
{
{
$project: {
_id: 0,
count: 1,
@@ -4211,6 +4228,25 @@ UserSchema.statics.calcOtherByUser = async function (idapp, userId) {
};
UserSchema.statics.tooManyReqPassword = async function (idapp, email, set) {
const User = this;
const maxnum = 30;
const user = await User.findByEmail(idapp, email);
if (user) {
if (!user.retry_pwd)
user.retry_pwd = 0
if (set && user.retry_pwd <= maxnum) {
user.retry_pwd++;
await User.findOneAndUpdate({ _id: user._id }, { $set: { retry_pwd: user.retry_pwd } });
}
return user.retry_pwd > maxnum ;
}
};
UserSchema.statics.createNewSubRecord = async function (idapp, req) {
const User = this;