diff --git a/src/users/users.cpp b/src/users/users.cpp index 42eb212..bb1b794 100644 --- a/src/users/users.cpp +++ b/src/users/users.cpp @@ -126,7 +126,7 @@ void add_user(const httplib::Request& request, httplib::Response& response) { all_users.push_back(nlohmann::json::parse(new_user_data)); std::ofstream usersfilew("users.json"); - usersfilew << all_users.dump(); + usersfilew << all_users.dump(4); usersfilew.close(); response.set_content("{\"status\":\"ok\"}", "application/json"); @@ -176,7 +176,7 @@ void del_user(const httplib::Request& request, httplib::Response& response) { } if (!found) {response.set_content("{\"status\":\"can't find this user\"}", "application/json");return;} std::ofstream usersfilew("users.json"); - usersfilew << all_users.dump(); + usersfilew << all_users.dump(4); usersfilew.close(); response.set_content("{\"status\":\"ok\"}", "application/json"); } @@ -252,7 +252,7 @@ void change_password(const httplib::Request& request, httplib::Response& respons } if (!found) {response.set_content("{\"status\":\"can't find this user\"}", "application/json");return;} std::ofstream usersfilew("users.json"); - usersfilew << all_users.dump(); + usersfilew << all_users.dump(4); usersfilew.close(); response.set_content("{\"status\":\"ok\"}", "application/json"); } diff --git a/src/web/index.html b/src/web/index.html index 888980c..3dc2e09 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -14,6 +14,7 @@
+

diff --git a/src/web/js/add_user.js b/src/web/js/add_user.js index bb87e7a..13c321e 100644 --- a/src/web/js/add_user.js +++ b/src/web/js/add_user.js @@ -24,16 +24,19 @@ adduserformbody.addEventListener('submit', async (e) => { group: groupinput.value, is_superuser: is_superuserinput.checked?"1":"0" } - console.log({...new_user, token: localStorage.getItem('token')}) const status = await fetch(routes.adduser(), { method: 'POST', body: `{"token":"${localStorage.getItem('token')}","username":"${new_user.username}","password":"${new_user.password}","group":"${new_user.group}","is_superuser":"${new_user.is_superuser}"}` - }).then(data => data.json()).then(jsondata => jsondata) - console.log(status) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) if (status.status === "ok") { adduserform.classList.remove('active') get_all_users() notification("Пользователь успешно добавлен", "success") + usernameinput.value = "" + passwordinput.value = "" + groupinput.value = "" } if (status.status === "user already exists") { notification("Пользователь с таким именем уже существует", "error") diff --git a/src/web/js/change_password.js b/src/web/js/change_password.js index 61edb13..c3ebd53 100644 --- a/src/web/js/change_password.js +++ b/src/web/js/change_password.js @@ -20,10 +20,12 @@ changepasswordformbody.addEventListener('submit', async e => { const status = await fetch(routes.change_password_url(), { method: 'POST', body: `{"token":"${localStorage.getItem('token')}","userid":"${changepassuserid}","new_password":"${changepasswordnewpassword.value}"}` - }).then(data => data.json()).then(jsondata => jsondata) - console.log(status) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) if (status.status === "ok") { notification("Пароль успешно изменен", "success") changepasswordform.classList.remove('active') + changepasswordnewpassword.value = "" } }) diff --git a/src/web/js/del_user.js b/src/web/js/del_user.js index ff386f3..18ac2ac 100644 --- a/src/web/js/del_user.js +++ b/src/web/js/del_user.js @@ -24,10 +24,12 @@ confirmformok.addEventListener('click', async () => { const status = await fetch(routes.deluser(), { method: 'POST', body: `{"token":"${localStorage.getItem('token')}","userid":"${useridtodelete}"}` - }).then(data => data.json()).then(jsondata => jsondata) - - console.log(status) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) if (status.status === "ok") { + confirmform.classList.remove('active') + get_all_users() notification("Пользователь успешно удален", "success") } }) diff --git a/src/web/js/get_all_users.js b/src/web/js/get_all_users.js index fa4cf1f..09d5d4a 100644 --- a/src/web/js/get_all_users.js +++ b/src/web/js/get_all_users.js @@ -2,8 +2,9 @@ const get_all_users = async () => { const response = await fetch(routes.all_users(), { method: "POST", body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => jsondata) - console.log(response) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) const alluserstablebody = document.querySelector('.alluserstablebody') alluserstablebody.innerHTML = "" for (const user of response) { diff --git a/src/web/js/login.js b/src/web/js/login.js index 7ed3c70..e5bb295 100644 --- a/src/web/js/login.js +++ b/src/web/js/login.js @@ -4,13 +4,11 @@ const loginforminputusername = document.querySelector('#loginforminputusername') const loginforminputpassword = document.querySelector('#loginforminputpassword') const loginformbutton = document.querySelector('.loginformbutton') const exitbutton = document.querySelector('.exitbutton') - -// loginformbody.unbind('submit') +const usernameheader = document.querySelector('.usernameheader') loginformbody.addEventListener('submit', (e) => { e.preventDefault() login(loginforminputusername.value, loginforminputpassword.value) - // console.log(loginform, loginforminputpassword, loginforminputusername, loginformbutton) }) const login = async (username, password) => { @@ -23,7 +21,13 @@ const login = async (username, password) => { }, // mode: 'no-cors', body: `{"username":"${username}","password":"${password}"}` - }).then(data => data.json()).then(jsondata => jsondata) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + if (error.message === "Unexpected end of JSON input") { + notification(`Ошибка на сервере: Создайте файл users.json и запишите в него первого пользователя`, "error") + } else { + notification(`Ошибка на сервере: ${error}`, "error") + } + }) if (response.status === "403") { notification("Неправильный логин или пароль", "error") } @@ -32,6 +36,8 @@ const login = async (username, password) => { loginform.classList.remove("active") localStorage.setItem("token", response.token) get_user_data(response.token) + loginforminputusername.value = "" + loginforminputpassword.value = "" } } @@ -39,9 +45,12 @@ const get_user_data = async (token) => { const response = await fetch(routes.user(), { method: "POST", body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => jsondata) + }).then(data => data.json()).then(jsondata => jsondata).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) if (response.is_superuser === "1") { get_all_users() + usernameheader.innerHTML = response.username } else { document.querySelector('.alluserstablebody').innerHTML = "" loginform.classList.add("active") diff --git a/src/web/js/window.js b/src/web/js/window.js index 7a6f728..4c29ec6 100644 --- a/src/web/js/window.js +++ b/src/web/js/window.js @@ -10,7 +10,10 @@ document.addEventListener("DOMContentLoaded", () => { } else { document.querySelector('.loginform').classList.add('active') } + }).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") }) + usernameheader.innerHTML = JSON.parse(localStorage.getItem('userdata')).username } else { document.querySelector('.loginform').classList.add('active') }