276 lines
10 KiB
Plaintext
276 lines
10 KiB
Plaintext
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Discord Clone - Sign Up</title>
|
|
<link rel="stylesheet" href="static/css/index.css"/>
|
|
</head>
|
|
<body>
|
|
<div class="signup-container">
|
|
<div class="signup-header">
|
|
<div class="logo">DC</div>
|
|
<h1>Create Account</h1>
|
|
<p>Join the conversation today</p>
|
|
</div>
|
|
|
|
<div class="signup-form">
|
|
<div class="success-message" id="successMessage">
|
|
Account created successfully! Redirecting...
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="username">Username</label>
|
|
<input
|
|
type="text"
|
|
id="username"
|
|
name="username"
|
|
placeholder="Enter your username"
|
|
required
|
|
/>
|
|
<div class="error-message">Username is required</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input
|
|
type="email"
|
|
id="email"
|
|
name="email"
|
|
placeholder="Enter your email"
|
|
required
|
|
/>
|
|
<div class="error-message">Please enter a valid email</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<div class="input-wrapper">
|
|
<input
|
|
type="password"
|
|
id="password"
|
|
name="password"
|
|
placeholder="Enter your password"
|
|
required
|
|
/>
|
|
<button
|
|
type="button"
|
|
class="password-toggle"
|
|
id="passwordToggle"
|
|
>
|
|
SHOW
|
|
</button>
|
|
</div>
|
|
<div class="error-message">
|
|
Password must be at least 8 characters
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="confirmPassword">Confirm Password</label>
|
|
<div class="input-wrapper">
|
|
<input
|
|
type="password"
|
|
id="confirmPassword"
|
|
name="confirmPassword"
|
|
placeholder="Confirm your password"
|
|
required
|
|
/>
|
|
<button
|
|
type="button"
|
|
class="password-toggle"
|
|
id="confirmPasswordToggle"
|
|
>
|
|
SHOW
|
|
</button>
|
|
</div>
|
|
<div class="error-message">Passwords do not match</div>
|
|
</div>
|
|
|
|
<div class="checkbox-group">
|
|
<input type="checkbox" id="terms" name="terms" required />
|
|
<label for="terms">
|
|
I agree to the <a href="#">Terms of Service</a> and
|
|
<a href="#">Privacy Policy</a>
|
|
</label>
|
|
</div>
|
|
|
|
<button type="button" class="submit-button" id="submitButton">
|
|
Create Account
|
|
</button>
|
|
</div>
|
|
|
|
<div class="signup-footer">
|
|
Already have an account? <a href="/login">Login</a>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const form = {
|
|
username: document.getElementById("username"),
|
|
email: document.getElementById("email"),
|
|
password: document.getElementById("password"),
|
|
confirmPassword: document.getElementById("confirmPassword"),
|
|
terms: document.getElementById("terms"),
|
|
};
|
|
|
|
const submitButton = document.getElementById("submitButton");
|
|
const successMessage = document.getElementById("successMessage");
|
|
|
|
// Password toggle functionality
|
|
const passwordToggle = document.getElementById("passwordToggle");
|
|
const confirmPasswordToggle = document.getElementById(
|
|
"confirmPasswordToggle",
|
|
);
|
|
|
|
passwordToggle.addEventListener("click", function () {
|
|
const type =
|
|
form.password.type === "password" ? "text" : "password";
|
|
form.password.type = type;
|
|
this.textContent = type === "password" ? "SHOW" : "HIDE";
|
|
});
|
|
|
|
confirmPasswordToggle.addEventListener("click", function () {
|
|
const type =
|
|
form.confirmPassword.type === "password"
|
|
? "text"
|
|
: "password";
|
|
form.confirmPassword.type = type;
|
|
this.textContent = type === "password" ? "SHOW" : "HIDE";
|
|
});
|
|
|
|
// Validation functions
|
|
function validateUsername() {
|
|
const value = form.username.value.trim();
|
|
const isValid = value.length >= 3;
|
|
toggleError(form.username, !isValid);
|
|
return isValid;
|
|
}
|
|
|
|
function validateEmail() {
|
|
const value = form.email.value.trim();
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
const isValid = emailRegex.test(value);
|
|
toggleError(form.email, !isValid);
|
|
return isValid;
|
|
}
|
|
|
|
function validatePassword() {
|
|
const value = form.password.value;
|
|
const isValid = value.length >= 8;
|
|
toggleError(form.password, !isValid);
|
|
return isValid;
|
|
}
|
|
|
|
function validateConfirmPassword() {
|
|
const isValid =
|
|
form.confirmPassword.value === form.password.value &&
|
|
form.confirmPassword.value.length > 0;
|
|
toggleError(form.confirmPassword, !isValid);
|
|
return isValid;
|
|
}
|
|
|
|
function toggleError(input, hasError) {
|
|
const formGroup = input.closest(".form-group");
|
|
if (hasError) {
|
|
formGroup.classList.add("error");
|
|
} else {
|
|
formGroup.classList.remove("error");
|
|
}
|
|
}
|
|
|
|
// Add blur validation
|
|
form.username.addEventListener("blur", validateUsername);
|
|
form.email.addEventListener("blur", validateEmail);
|
|
form.password.addEventListener("blur", validatePassword);
|
|
form.confirmPassword.addEventListener(
|
|
"blur",
|
|
validateConfirmPassword,
|
|
);
|
|
|
|
// Form submission
|
|
submitButton.addEventListener("click", async function () {
|
|
// Validate all fields
|
|
const isUsernameValid = validateUsername();
|
|
const isEmailValid = validateEmail();
|
|
const isPasswordValid = validatePassword();
|
|
const isConfirmPasswordValid = validateConfirmPassword();
|
|
const areTermsAccepted = form.terms.checked;
|
|
|
|
if (
|
|
!isUsernameValid ||
|
|
!isEmailValid ||
|
|
!isPasswordValid ||
|
|
!isConfirmPasswordValid ||
|
|
!areTermsAccepted
|
|
) {
|
|
if (!areTermsAccepted) {
|
|
alert("Please accept the Terms of Service");
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Disable button and show loading
|
|
submitButton.disabled = true;
|
|
submitButton.innerHTML =
|
|
'<span class="loading-spinner"></span>Creating Account...';
|
|
|
|
// Prepare data
|
|
const formData = {
|
|
username: form.username.value.trim(),
|
|
// email: form.email.value.trim(),
|
|
password: form.password.value,
|
|
};
|
|
|
|
try {
|
|
// Replace with your actual backend endpoint
|
|
const response = await fetch(
|
|
"/api/signup",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(formData),
|
|
},
|
|
);
|
|
|
|
if (response.ok) {
|
|
// Show success message
|
|
successMessage.classList.add("show");
|
|
submitButton.innerHTML = "Account Created!";
|
|
|
|
// Optional: Redirect after success
|
|
setTimeout(() => {
|
|
window.location.href = '/chat';
|
|
// console.log("Redirecting to chat...");
|
|
}, 2000);
|
|
} else {
|
|
const error = await response.json();
|
|
throw new Error(error.message || "Signup failed");
|
|
}
|
|
} catch (error) {
|
|
console.error("Signup error:", error);
|
|
alert(
|
|
error.message ||
|
|
"Failed to create account. Please try again.",
|
|
);
|
|
submitButton.disabled = false;
|
|
submitButton.innerHTML = "Create Account";
|
|
}
|
|
});
|
|
|
|
// Allow Enter key to submit
|
|
Object.values(form).forEach((input) => {
|
|
if (input.tagName === "INPUT") {
|
|
input.addEventListener("keypress", function (e) {
|
|
if (e.key === "Enter") {
|
|
submitButton.click();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|