From 8e3798be21cb2c58ffc2c7dca41cc300a4772d47 Mon Sep 17 00:00:00 2001 From: Balgyn Date: Fri, 21 Aug 2026 10:58:57 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=83=D0=B1=D0=BB=D0=B8=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index c16f0ea..d6f0be3 100644 --- a/server.js +++ b/server.js @@ -149,15 +149,22 @@ http.createServer(async (req, res) => { // API: Логин if (req.method === "POST" && url === "/api/login") { - const data = readData(); - const { login, password } = await body(req); - const user = data.users.find(u => u.login === login && u.password === password); - if (!user) { - res.writeHead(401, { "Content-Type": "application/json" }); - return res.end(JSON.stringify({ ok: false, error: "Неверный логин или пароль" })); + try { + const data = readData(); + const creds = await body(req); + const login = creds.login; + const password = creds.password; + const user = data.users.find(u => u.login === login && u.password === password); + if (!user) { + res.writeHead(401, { "Content-Type": "application/json" }); + return res.end(JSON.stringify({ ok: false, error: "Неверный логин или пароль" })); + } + res.writeHead(200, { "Content-Type": "application/json" }); + return res.end(JSON.stringify({ ok: true, user: { login: user.login, name: user.name, role: user.role } })); + } catch (e) { + res.writeHead(500, { "Content-Type": "application/json" }); + return res.end(JSON.stringify({ ok: false, error: "Ошибка сервера: " + e.message })); } - res.writeHead(200, { "Content-Type": "application/json" }); - return res.end(JSON.stringify({ ok: true, user: { login: user.login, name: user.name, role: user.role } })); } // API: Logout (просто для вида)