ваше сообщение коммита

This commit is contained in:
2025-07-27 03:30:13 +03:00
parent 057fe6254c
commit 1835632be9
141 changed files with 32514 additions and 6661 deletions

View File

@@ -10,7 +10,7 @@
* GitHub: https://github.com/HB3-ACCELERATOR
*/
const db = require('../db');
const encryptedDb = require('./encryptedDatabaseService');
const logger = require('../utils/logger');
class VerificationService {
@@ -39,23 +39,21 @@ class VerificationService {
`Creating verification code for ${provider}:${providerId}, userId: ${userId || 'null'}`
);
// Если userId не указан, добавляем запись без ссылки на пользователя
if (userId === null || userId === undefined) {
await db.getQuery()(
`INSERT INTO verification_codes
(code, provider, provider_id, expires_at)
VALUES ($1, $2, $3, $4)`,
[code, provider, providerId, expiresAt]
);
} else {
await db.getQuery()(
`INSERT INTO verification_codes
(code, provider, provider_id, user_id, expires_at)
VALUES ($1, $2, $3, $4, $5)`,
[code, provider, providerId, userId, expiresAt]
);
const data = {
code: code,
provider: provider,
provider_id: providerId,
expires_at: expiresAt,
used: false
};
// Если userId указан, добавляем его
if (userId !== null && userId !== undefined) {
data.user_id = userId;
}
await encryptedDb.saveData('verification_codes', data);
logger.info(`Verification code created successfully for ${provider}:${providerId}`);
return code;
} catch (error) {
@@ -79,53 +77,52 @@ class VerificationService {
logger.info(`Normalized code: ${normalizedCode}`);
// Проверим, есть ли такой код в базе (для отладки)
const checkResult = await db.getQuery()(
`SELECT code FROM verification_codes
WHERE provider = $1
AND provider_id = $2
AND used = false
AND expires_at > NOW()`,
[provider, providerId]
);
const checkResult = await encryptedDb.getData('verification_codes', {
provider: provider,
provider_id: providerId,
used: false
});
if (checkResult.rows.length > 0) {
if (checkResult.length > 0) {
logger.info(
`Found codes for ${provider}:${providerId}: ${JSON.stringify(checkResult.rows.map((r) => r.code))}`
`Found codes for ${provider}:${providerId}: ${JSON.stringify(checkResult.map((r) => r.code))}`
);
} else {
logger.warn(`No active codes found for ${provider}:${providerId}`);
}
const result = await db.getQuery()(
`SELECT * FROM verification_codes
WHERE code = $1
AND provider = $2
AND provider_id = $3
AND used = false
AND expires_at > NOW()`,
[normalizedCode, provider, providerId]
);
const result = await encryptedDb.getData('verification_codes', {
code: normalizedCode,
provider: provider,
provider_id: providerId,
used: false
}, 1);
if (result.rows.length === 0) {
logger.warn(
`Invalid or expired code for ${provider}:${providerId}. Input: ${normalizedCode}`
);
return { success: false, error: 'Неверный или истекший код' };
if (result.length === 0) {
logger.warn(`No valid verification code found for ${provider}:${providerId}`);
return { valid: false, message: 'Invalid or expired code' };
}
const verification = result.rows[0];
const verificationCode = result[0];
// Проверяем срок действия
if (new Date(verificationCode.expires_at) < new Date()) {
logger.warn(`Verification code expired for ${provider}:${providerId}`);
return { valid: false, message: 'Code has expired' };
}
// Отмечаем код как использованный
await db.getQuery()(
'UPDATE verification_codes SET used = true WHERE id = $1',
[verification.id]
);
await encryptedDb.saveData('verification_codes', {
used: true
}, {
id: verificationCode.id
});
logger.info(`Code verified successfully for ${provider}:${providerId}`);
logger.info(`Verification code verified successfully for ${provider}:${providerId}`);
return {
success: true,
userId: verification.user_id,
providerId: verification.provider_id,
valid: true,
userId: verificationCode.user_id,
message: 'Code verified successfully'
};
} catch (error) {
logger.error('Error verifying code:', {
@@ -141,15 +138,19 @@ class VerificationService {
// Очистка истекших кодов
async cleanupExpiredCodes() {
try {
const result = await db.getQuery()(
'DELETE FROM verification_codes WHERE expires_at <= NOW() RETURNING id'
);
logger.info(`Cleaned up ${result.rowCount} expired verification codes`);
const expiredCodes = await encryptedDb.getData('verification_codes', {
expires_at: { $lt: new Date() }
});
for (const code of expiredCodes) {
await encryptedDb.deleteData('verification_codes', { id: code.id });
}
logger.info(`Cleaned up ${expiredCodes.length} expired verification codes`);
} catch (error) {
logger.error('Error cleaning up expired codes:', error);
}
}
}
const verificationService = new VerificationService();
module.exports = verificationService;
module.exports = new VerificationService();