55 lines
1.5 KiB
JavaScript
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();
|
|
});
|