Опубликовано через кнопку
This commit is contained in:
parent
bd721f2c66
commit
8e3798be21
@ -149,8 +149,11 @@ http.createServer(async (req, res) => {
|
||||
|
||||
// API: Логин
|
||||
if (req.method === "POST" && url === "/api/login") {
|
||||
try {
|
||||
const data = readData();
|
||||
const { login, password } = await body(req);
|
||||
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" });
|
||||
@ -158,6 +161,10 @@ http.createServer(async (req, res) => {
|
||||
}
|
||||
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 }));
|
||||
}
|
||||
}
|
||||
|
||||
// API: Logout (просто для вида)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user