kittemail/kittemail-prefork/resources/static/js/settings.js

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-12-10 19:54:22 +00:00
document.addEventListener("DOMContentLoaded", async () => {
let loginCode = ""
let emails = await fetch("/api/email/list", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: localStorage.getItem("SECRET-token")
})
})
if (emails.status !== 200) {
window.location.href = "/signup";
}
let address, username
emails = await emails.json()
emails.forEach(email => {
if (email["isPrimary"]) {
address = email["address"]
username = email["username"]
}
})
document.getElementById("copyLoginCode").addEventListener("click", () => {
navigator.clipboard.writeText(loginCode).then(() => {
document.getElementById("copyLoginCode").innerText = "Copied!"
setTimeout(() => {
document.getElementById("copyLoginCode").innerText = "Copy login code"
}, 2000)
})
})
document.getElementById("closeLoginCode").addEventListener("click", () => {
document.getElementById("loginCodeDisplay").style.display = "none";
})
document.getElementById("showLoginCode").addEventListener("click", () => {
document.getElementById("loginCodeDisplay").style.display = "flex";
})
document.getElementById("resetLoginCode").addEventListener("click", async () => {
await fetch("/api/email/resetLoginCode", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: localStorage.getItem("SECRET-token"),
address: address,
username: username
})
})
})
document.getElementById("deleteAccount").addEventListener("click", async () => {
await fetch("/api/email/delete", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: localStorage.getItem("SECRET-token"),
address: address,
username: username
})
})
localStorage.removeItem("SECRET-token")
window.location.href = "/oauth";
})
})