"use strict"; exports.go = async (req, res, postData, urlParsed) => { try { console.log('[api/auth-server] Авторизация через внешний сервер:', postData); // Проверяем соединение с внешним сервером if (!global.conn_to_server) { console.error('[api/auth-server] Нет соединения с внешним сервером'); return res.end(JSON.stringify({ success: false, error: "Нет соединения с сервером" })); } if (!postData || !postData.phone || !postData.password) { return res.end(JSON.stringify({ success: false, error: "Не указан номер телефона или пароль" })); } // Нормализуем номер телефона const normalizedPhone = postData.phone.replace(/\D/g, ''); console.log('[api/auth-server] Попытка авторизации через сервер для:', normalizedPhone); // ПРОБУЕМ АВТОРИЗАЦИЮ НА ВНЕШНЕМ СЕРВЕРЕ БЕЗ serialcpu // Может быть есть отдельный метод для авторизации пользователей const authResponse = await global.send_to_server({ do: "user/auth", // Попробуем другой метод tel: normalizedPhone, pass: postData.password }); console.log('[api/auth-server] Ответ от сервера (user/auth):', JSON.stringify(authResponse, null, 2)); if (authResponse.err) { // Если user/auth не работает, попробуем старый метод avt console.log('[api/auth-server] Пробуем метод avt...'); const authResponse2 = await global.send_to_server({ do: "avt", tel: normalizedPhone, pass: postData.password // НЕ отправляем serialcpu - может сработать для пользователей }); console.log('[api/auth-server] Ответ от сервера (avt без serialcpu):', JSON.stringify(authResponse2, null, 2)); if (authResponse2.err) { return res.end(JSON.stringify({ success: false, error: "Ошибка авторизации на сервере: " + authResponse2.err })); } // Если авторизация успешна if (authResponse2.user) { return res.end(JSON.stringify({ success: true, user: { id: authResponse2.user._id || normalizedPhone, name: authResponse2.user.fio || authResponse2.user.name || 'Пользователь', phone: normalizedPhone, role: authResponse2.user.taccess || 'operator', isAuthenticated: true, serverAuth: true, serverConnected: true } })); } } else if (authResponse.user) { // user/auth сработал return res.end(JSON.stringify({ success: true, user: { id: authResponse.user._id || normalizedPhone, name: authResponse.user.fio || authResponse.user.name || 'Пользователь', phone: normalizedPhone, role: authResponse.user.taccess || 'operator', isAuthenticated: true, serverAuth: true, serverConnected: true } })); } return res.end(JSON.stringify({ success: false, error: "Неверный логин или пароль" })); } catch (error) { console.error('[api/auth-server] Критическая ошибка:', error); res.end(JSON.stringify({ success: false, error: "Внутренняя ошибка сервера" })); } };