Files
msgs/web/js/register.js
T
wt b833266664
Continuous integration / Check (push) Successful in 35s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / build (push) Successful in 30s
Continuous integration / build-release (push) Successful in 51s
more refactor in frontend javascript
2025-11-28 02:28:52 +07:00

55 lines
1.5 KiB
JavaScript

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();
});