Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions controller/authController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
const authService = require('../services/authService');
const { isServiceError } = require('../services/serviceError');

const TRUSTED_DEVICE_COOKIE = authService.trustedDeviceCookieName || 'trusted_device';

function getDeviceInfo(req) {
return {
ip: req.ip,
userAgent: req.get('User-Agent') || 'Unknown'
};
}

function clearTrustedDeviceCookie(res) {
if (!res?.clearCookie) {
return;
}

res.clearCookie(TRUSTED_DEVICE_COOKIE, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/'
});
}

function handleServiceError(res, error, fallbackStatus, fallbackLogLabel) {
if (isServiceError(error)) {
return res.status(error.statusCode).json({
Expand Down Expand Up @@ -72,13 +87,37 @@ exports.logout = async (req, res) => {

exports.logoutAll = async (req, res) => {
try {
const result = await authService.logoutAll(req.user.userId);
const result = await authService.logoutAll(req.user.userId, {
reason: 'logout_all',
deviceInfo: getDeviceInfo(req)
});

clearTrustedDeviceCookie(res);
return res.json(result);
} catch (error) {
return handleServiceError(res, error, 500, 'Logout all error:');
}
};

exports.revokeTrustedDevices = async (req, res) => {
try {
const result = await authService.revokeTrustedDevices(
req.user.userId,
'manual',
getDeviceInfo(req)
);

clearTrustedDeviceCookie(res);
return res.json({
success: true,
message: 'Trusted devices revoked successfully',
revokedCount: result.revokedCount
});
} catch (error) {
return handleServiceError(res, error, 500, 'Revoke trusted devices error:');
}
};

exports.getProfile = async (req, res) => {
try {
const result = await authService.getProfile(req.user.userId);
Expand All @@ -104,7 +143,7 @@ exports.logLoginAttempt = async (req, res) => {
return res.status(error.statusCode).json({ error: error.message });
}

console.error('Failed to insert login log:', error);
console.error('Failed to insert login log:', error);
return res.status(500).json({ error: 'Failed to log login attempt' });
}
};
Expand All @@ -118,7 +157,7 @@ exports.sendSMSByEmail = async (req, res) => {
return res.status(error.statusCode).json({ error: error.message });
}

console.error('Error sending SMS:', error);
console.error('Error sending SMS:', error);
return res.status(500).json({ error: 'Internal server error' });
}
};
Loading
Loading