- corretto problema ROGNOSO : Risolvere la questione "Sessioni multiple", se apro 2 browser l'ultimo va a cancellare il precedente, e mi da errore di email non valida !
Il problema era sulla fetch nel service worker, gestita in quel modo personalizzato, andava in conflitto, non tenendo le chiamate bloccanti, ma uscivano prima che arrivasse la risposta del server. - Per chi è da tanto che non si collega a RISO, compare "Email non verificata"... (si risolve chiudendo su ESCI e riloggandosi)... però andrebbe sistemata. (stesso problema di prima).
This commit is contained in:
@@ -551,7 +551,6 @@ UserSchema.methods.toJSON = function () {
|
||||
};
|
||||
|
||||
UserSchema.methods.generateAuthToken = function (req) {
|
||||
// console.log("GENERA TOKEN : ");
|
||||
const user = this;
|
||||
|
||||
const useragent = req.get('User-Agent');
|
||||
@@ -568,34 +567,37 @@ UserSchema.methods.generateAuthToken = function (req) {
|
||||
|
||||
if (attiva_scadenza)
|
||||
token = jwt
|
||||
.sign({ _id: prova, smart: user._id.toHexString(), access, un: user.username }, process.env.SIGNCODE, {
|
||||
.sign({ _id: user._id.toHexString(), access, un: user.username }, process.env.SIGNCODE, {
|
||||
expiresIn: numsec,
|
||||
})
|
||||
.toString();
|
||||
else
|
||||
token = jwt
|
||||
.sign({ _id: prova, smart: user._id.toHexString(), access, un: user.username }, process.env.SIGNCODE)
|
||||
.sign({ _id: user._id.toHexString(), access, un: user.username }, process.env.SIGNCODE)
|
||||
.toString();
|
||||
|
||||
const refreshToken = jwt
|
||||
.sign({ _id: prova, smart: user._id.toHexString(), access, un: user.username }, process.env.SECRK, {
|
||||
.sign({ _id: user._id.toHexString(), access, un: user.username }, process.env.SECRK, {
|
||||
expiresIn: process.env.REFRESH_TOKEN_LIFE,
|
||||
})
|
||||
.toString();
|
||||
const date_login = new Date();
|
||||
|
||||
// CANCELLA IL PRECEDENTE !
|
||||
user.tokens = user.tokens.filter(function (tok) {
|
||||
return tok.access !== access || (tok.access === access && tok.browser !== browser);
|
||||
});
|
||||
|
||||
user.tokens.push({ access, browser, token, date_login, refreshToken });
|
||||
// Controlla se il token è già presente per la coppia access-browser
|
||||
const idx = user.tokens.findIndex((tok) => tok.access === access && tok.browser === browser);
|
||||
if (idx === -1) {
|
||||
user.tokens.push({ access, browser, token, date_login, refreshToken });
|
||||
} else {
|
||||
// Se il token esiste già, sostituisce il valore vecchio con il nuovo
|
||||
user.tokens[idx] = { access, browser, token, date_login, refreshToken };
|
||||
}
|
||||
|
||||
user.lasttimeonline = new Date();
|
||||
|
||||
return user
|
||||
.save()
|
||||
.then(() => {
|
||||
console.log('########## HO CREATO UN NUOVO TOKEN E REFRESHTOKEN !!!!! ----------- ');
|
||||
return { token, refreshToken };
|
||||
})
|
||||
.catch((err) => {
|
||||
@@ -811,7 +813,7 @@ UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, wi
|
||||
if (withlean) {
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded.smart,
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
@@ -824,7 +826,7 @@ UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, wi
|
||||
} else {
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded.smart,
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
@@ -852,7 +854,7 @@ UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, wi
|
||||
const start_find = process.hrtime.bigint();
|
||||
user = await User.findOne(
|
||||
{
|
||||
_id: decoded.smart,
|
||||
_id: decoded._id,
|
||||
tokens: {
|
||||
$elemMatch: {
|
||||
token,
|
||||
@@ -893,11 +895,28 @@ UserSchema.statics.findByTokenAnyAccess = function (token) {
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
_id: decoded.smart,
|
||||
_id: decoded._id,
|
||||
'tokens.token': token,
|
||||
}).lean();
|
||||
};
|
||||
|
||||
UserSchema.statics.findByRefreshTokenAnyAccess = function (refreshToken) {
|
||||
const User = this;
|
||||
let decoded;
|
||||
|
||||
try {
|
||||
decoded = jwt.verify(refreshToken, process.env.SECRK);
|
||||
} catch (e) {
|
||||
console.error('Err findByRefreshTokenAnyAccess:', e);
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
_id: decoded._id,
|
||||
'tokens.refreshToken': refreshToken,
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.findByCredentials = async function (idapp, username, password, pwdcrypted) {
|
||||
const User = this;
|
||||
let pwd = '';
|
||||
@@ -1583,7 +1602,7 @@ UserSchema.statics.createNewRequestPwd = function (idapp, email, code) {
|
||||
} else {
|
||||
// Creo il tokenforgot
|
||||
user.tokenforgot = jwt
|
||||
.sign({ _id: 'prova123##', smart: user._id.toHexString() }, process.env.SIGNCODE)
|
||||
.sign({ _id: user._id.toHexString() }, process.env.SIGNCODE)
|
||||
.toString();
|
||||
user.date_tokenforgot = new Date();
|
||||
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
|
||||
@@ -1621,7 +1640,7 @@ UserSchema.statics.createNewRequestPwdByUsernameAndGetLink = async function (ida
|
||||
const prova = 'dasdas1231#11';
|
||||
// Creo il tokenforgot
|
||||
user.tokenforgot = jwt
|
||||
.sign({ _id: prova, smart: user._id.toHexString(), ...additionalData }, process.env.SIGNCODE)
|
||||
.sign({ _id: user._id.toHexString(), ...additionalData }, process.env.SIGNCODE)
|
||||
.toString();
|
||||
user.date_tokenforgot = new Date();
|
||||
user.tokenforgot_code = 100000 + Math.round(Math.random() * 899999);
|
||||
|
||||
Reference in New Issue
Block a user