54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
const API_BASE = "https://mail.pandem.fr/api/v1";
|
|
const LOGIN_ENDPOINT = "/login";
|
|
const GET_ALIAS_ENDPOINT = "/get/alias";
|
|
const ADD_ALIAS_ENDPOINT = "/add/alias";
|
|
|
|
// Check if user is logged in
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
const storedSession = await browser.storage.local.get("session");
|
|
if (storedSession.session) {
|
|
showEmailForm();
|
|
}
|
|
});
|
|
|
|
document.getElementById("login").addEventListener("click", async () => {
|
|
const username = document.getElementById("username").value;
|
|
const password = document.getElementById("password").value;
|
|
|
|
try {
|
|
const response = await fetch(API_BASE + LOGIN_ENDPOINT, {
|
|
method: "POST",
|
|
credentials: "include",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
if (!response.ok) throw new Error("Login failed");
|
|
|
|
await browser.storage.local.set({ session: true });
|
|
showEmailForm();
|
|
} catch (error) {
|
|
console.error(error);
|
|
alert("Login error");
|
|
}
|
|
});
|
|
|
|
document.getElementById("logout").addEventListener("click", async () => {
|
|
await browser.storage.local.remove("session");
|
|
showLoginForm();
|
|
});
|
|
|
|
document.getElementById("fetch").addEventListener("click", async () => {
|
|
try {
|
|
const response = await fetch(API_BASE + GET_ALIAS_ENDPOINT, {
|
|
credentials: "include"
|
|
});
|
|
|
|
if (!response.ok) throw new Error("Failed to fetch emails");
|
|
|
|
const aliases = await response.json();
|
|
|
|
if (aliases.length > 0) {
|
|
const latestEmail = aliases[0].address;
|
|
document.getElementById("email").value = latestEmail;
|
|
}
|