-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogin.js
More file actions
58 lines (50 loc) · 1.77 KB
/
Copy pathlogin.js
File metadata and controls
58 lines (50 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { showMessage } from "/js/shared.js";
import { isWebAuthnSupported } from "/js/user/webauthn-utils.js";
import { authenticateWithPasskey } from "/js/user/webauthn-authenticate.js";
document.addEventListener("DOMContentLoaded", () => {
const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
if (!validateForm(form)) {
event.preventDefault();
}
});
// Show passkey login button if WebAuthn is supported
const passkeySection = document.getElementById("passkey-login-section");
const passkeyBtn = document.getElementById("passkeyLoginBtn");
if (passkeySection && isWebAuthnSupported()) {
passkeySection.style.display = "block";
passkeyBtn.addEventListener("click", async () => {
passkeyBtn.disabled = true;
passkeyBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span> Authenticating...';
try {
const redirectUrl = await authenticateWithPasskey();
window.location.href = redirectUrl;
} catch (error) {
console.error("Passkey authentication failed:", error);
showMessage(null, "Passkey authentication failed. Please try again.", "alert-danger");
passkeyBtn.disabled = false;
passkeyBtn.innerHTML = '<i class="bi bi-key me-2"></i> Sign in with Passkey';
}
});
}
});
function validateForm(form) {
const username = form.username;
const password = form.password;
if (!username.value && !password.value) {
showMessage(null, "Both username and password are required.", "alert-danger");
username.focus();
return false;
}
if (!username.value) {
showMessage(null, "Username is required.", "alert-danger");
username.focus();
return false;
}
if (!password.value) {
showMessage(null, "Password is required.", "alert-danger");
password.focus();
return false;
}
return true;
}