Files
chatapp/backend/templates/2fa.html.tera
T
2025-10-11 01:52:21 +01:00

113 lines
4.0 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">
<img src="/static/favicon.ico" class="logo"/>
<h1>2FA Setup</h1>
</div>
<div class="signup-form">
<div class="success-message" id="successMessage">
Login successful, Redirecting
</div>
<img id="qr-code" alt="QR Code" style="width: 100%; height: auto; filter: brightness(0.925) invert(1);">
<div class="form-group">
<input
type="text"
id="mfa_code"
inputmode="numeric"
pattern="[0-9]*"
maxlength="6"
placeholder="000000"
style="font-size: 24px; letter-spacing: 0.5em; text-align: center; width: 100%;"
>
</div>
<button type="button" class="submit-button" id="submitButton">
Confirm!
</button>
</div>
</div>
<script>
const form = {
code: document.getElementById("mfa_code"),
};
submitButton.addEventListener("click", async function () {
submitButton.disabled = true;
submitButton.innerHTML =
'<span class="loading-spinner"></span>Creating Account...';
// Prepare data
const formData = {
code: form.code.value.trim(),
};
try {
// Replace with your actual backend endpoint
const response = await fetch(
"/api/totp",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
},
);
if (response.ok) {
// Show success message
successMessage.classList.add("show");
submitButton.innerHTML = "2FA Enabled";
// Optional: Redirect after success
setTimeout(() => {
window.location.href = '/chat';
// console.log("Redirecting to chat...");
}, 2000);
} else {
const error = await response.text();
throw new Error(error || "Login failed");
}
} catch (error) {
console.error("Login error:", error);
alert(
error.message ||
"Failed to login. Please try again.",
);
submitButton.disabled = false;
submitButton.innerHTML = "Login";
}
});
fetch('/api/totp.jpg')
.then(response => response.json())
.then(data => {
document.getElementById('qr-code').src = data.qr_code;
});
// 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>