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

* 🌟 feat: Implement Two-Factor Authentication (2FA) functionality * fix: Two-Factor Authentication Logic and State Management * 🌟 feat: Add LICENSE file and update package version to 0.0.2 with MIT license
30 lines
911 B
JavaScript
30 lines
911 B
JavaScript
const { generate2FATempToken } = require('~/server/services/twoFactorService');
|
|
const { setAuthTokens } = require('~/server/services/AuthService');
|
|
const { logger } = require('~/config');
|
|
|
|
const loginController = async (req, res) => {
|
|
try {
|
|
if (!req.user) {
|
|
return res.status(400).json({ message: 'Invalid credentials' });
|
|
}
|
|
|
|
if (req.user.twoFactorEnabled) {
|
|
const tempToken = generate2FATempToken(req.user._id);
|
|
return res.status(200).json({ twoFAPending: true, tempToken });
|
|
}
|
|
|
|
const { password: _p, totpSecret: _t, __v, ...user } = req.user;
|
|
user.id = user._id.toString();
|
|
|
|
const token = await setAuthTokens(req.user._id, res);
|
|
|
|
return res.status(200).send({ token, user });
|
|
} catch (err) {
|
|
logger.error('[loginController]', err);
|
|
return res.status(500).json({ message: 'Something went wrong' });
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
loginController,
|
|
};
|