LuggageTracker/static/verify.js
2025-07-17 12:17:47 -04:00

39 lines
1.5 KiB
JavaScript

let submitBtn = document.getElementById("submitBtn");
submitBtn.addEventListener('click', function(e) {
let code = document.getElementById("code").value.replaceAll(" ", "");
const path = window.location.pathname;
const parts = path.split("/");
const user = parts.pop().toLowerCase();
fetch("/api/verify/" + user, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': "Basic " + code,
}
}).then(response => response.json())
.then(data => {
let statusText = document.getElementById("status");
statusText.classList.remove("hidden");
console.log("Status Code: " + data.status);
console.log("Code Type: " + typeof data.status)
if (data.status === 404) {
// Not found
statusText.innerText = "User with that code not found";
} else if (data.status === 200) {
// Display found and redirect to baseUrl/u/user/info with auth token
statusText.innerText = "User found, redirecting...";
window.location.replace("/u/" + user + "/info?token=" + data.token);
} else {
// Error
statusText.innerText = "Error, please send this to Steven to be fixed. Error: " + data.error;
}
})
});
document.getElementById("code").addEventListener('keyup', function(e) {
if (e.key === "Enter") {
submitBtn.click();
}
});