mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* refactor: move model definitions and database-related methods to packages/data-schemas * ci: update tests due to new DB structure fix: disable mocking `librechat-data-provider` feat: Add schema exports to data-schemas package - Introduced a new schema module that exports various schemas including action, agent, and user schemas. - Updated index.ts to include the new schema exports for better modularity and organization. ci: fix appleStrategy tests fix: Agent.spec.js ci: refactor handleTools tests to use MongoMemoryServer for in-memory database fix: getLogStores imports ci: update banViolation tests to use MongoMemoryServer and improve session mocking test: refactor samlStrategy tests to improve mock configurations and user handling ci: fix crypto mock in handleText tests for improved accuracy ci: refactor spendTokens tests to improve model imports and setup ci: refactor Message model tests to use MongoMemoryServer and improve database interactions * refactor: streamline IMessage interface and move feedback properties to types/message.ts * refactor: use exported initializeRoles from `data-schemas`, remove api workspace version (this serves as an example of future migrations that still need to happen) * refactor: update model imports to use destructuring from `~/db/models` for consistency and clarity * refactor: remove unused mongoose imports from model files for cleaner code * refactor: remove unused mongoose imports from Share, Prompt, and Transaction model files for cleaner code * refactor: remove unused import in Transaction model for cleaner code * ci: update deploy workflow to reference new Docker Dev Branch Images Build and add new workflow for building Docker images on dev branch * chore: cleanup imports
60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
const jwt = require('jsonwebtoken');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
verifyTOTP,
|
|
getTOTPSecret,
|
|
verifyBackupCode,
|
|
} = require('~/server/services/twoFactorService');
|
|
const { setAuthTokens } = require('~/server/services/AuthService');
|
|
const { getUserById } = require('~/models');
|
|
|
|
/**
|
|
* Verifies the 2FA code during login using a temporary token.
|
|
*/
|
|
const verify2FAWithTempToken = async (req, res) => {
|
|
try {
|
|
const { tempToken, token, backupCode } = req.body;
|
|
if (!tempToken) {
|
|
return res.status(400).json({ message: 'Missing temporary token' });
|
|
}
|
|
|
|
let payload;
|
|
try {
|
|
payload = jwt.verify(tempToken, process.env.JWT_SECRET);
|
|
} catch (err) {
|
|
return res.status(401).json({ message: 'Invalid or expired temporary token' });
|
|
}
|
|
|
|
const user = await getUserById(payload.userId);
|
|
if (!user || !user.twoFactorEnabled) {
|
|
return res.status(400).json({ message: '2FA is not enabled for this user' });
|
|
}
|
|
|
|
const secret = await getTOTPSecret(user.totpSecret);
|
|
let isVerified = false;
|
|
if (token) {
|
|
isVerified = await verifyTOTP(secret, token);
|
|
} else if (backupCode) {
|
|
isVerified = await verifyBackupCode({ user, backupCode });
|
|
}
|
|
|
|
if (!isVerified) {
|
|
return res.status(401).json({ message: 'Invalid 2FA code or backup code' });
|
|
}
|
|
|
|
// Prepare user data to return (omit sensitive fields).
|
|
const userData = user.toObject ? user.toObject() : { ...user };
|
|
delete userData.password;
|
|
delete userData.__v;
|
|
delete userData.totpSecret;
|
|
userData.id = user._id.toString();
|
|
|
|
const authToken = await setAuthTokens(user._id, res);
|
|
return res.status(200).json({ token: authToken, user: userData });
|
|
} catch (err) {
|
|
logger.error('[verify2FAWithTempToken]', err);
|
|
return res.status(500).json({ message: 'Something went wrong' });
|
|
}
|
|
};
|
|
|
|
module.exports = { verify2FAWithTempToken };
|