some bug fixed

This commit is contained in:
wt committed 2024-05-24 15:46:22 +07:00
1 parent e310ed83b7
commit bff7788c70
8 files changed
+39 -18

No files matched your search

+3 -3
View File
@@ -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");
}
+1
View File
@@ -14,6 +14,7 @@
<div class="header">
<button class="exitbutton headerbutton">Exit</button>
<button class="adduserbutton headerbutton">Adduser</button>
<h1 class="usernameheader"></h1>
</div>
<div class="form loginform">
+6 -3
View File
@@ -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")
+4 -2
View File
@@ -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 = ""
}
})
+5 -3
View File
@@ -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")
}
})
+3 -2
View File
@@ -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) {
+14 -5
View File
@@ -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")
+3
View File
@@ -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')
}