more refactor in frontend javascript
This commit is contained in:
1 parent
7f7936d37f
commit
b833266664
14 files changed
+2345
-2366
No files matched your search
@@ -0,0 +1,54 @@
|
||||
async function register() {
|
||||
const phone = document.getElementById("registerPhone").value.trim();
|
||||
const username = document.getElementById("registerUsername").value.trim();
|
||||
const password = document.getElementById("registerPassword").value;
|
||||
|
||||
if (!phone || !username || !password) {
|
||||
showAlert("Please fill all fields", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
showAlert("Password must be at least 6 characters", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/register", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ phone, username, password }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
localStorage.setItem("chatToken", data.token);
|
||||
localStorage.setItem("chatUsername", data.username);
|
||||
localStorage.setItem("chatUserPhone", phone);
|
||||
|
||||
currentUser = data.username;
|
||||
currentToken = data.token;
|
||||
currentUserPhone = phone;
|
||||
|
||||
document.getElementById("authContainer").style.display = "none";
|
||||
document.getElementById("chatContainer").style.display = "flex";
|
||||
|
||||
updateUserProfile();
|
||||
loadChats();
|
||||
connectWebSocket();
|
||||
|
||||
showAlert("Registration successful!", "success");
|
||||
} else {
|
||||
showAlert(data.message, "error");
|
||||
}
|
||||
} catch (error) {
|
||||
showAlert("Registration failed. Please try again.", "error");
|
||||
}
|
||||
}
|
||||
|
||||
document
|
||||
.getElementById("registerPassword")
|
||||
.addEventListener("keypress", function (e) {
|
||||
if (e.key === "Enter") register();
|
||||
});
|
||||
Reference in new issue
Block a user