|
| 1 | +// Extension/Settings/Account/popup.js |
| 2 | + |
1 | 3 | document.addEventListener("DOMContentLoaded", () => { |
2 | | - const auth = firebase.auth(); |
| 4 | + if (typeof firebase === 'undefined') { |
| 5 | + console.error("Firebase is not initialized."); |
| 6 | + return; |
| 7 | + } |
3 | 8 |
|
| 9 | + // --- Get UI Elements --- |
4 | 10 | const nameInput = document.getElementById("name"); |
5 | 11 | const emailInput = document.getElementById("email"); |
6 | 12 | const passwordInput = document.getElementById("password"); |
7 | 13 | const messageDiv = document.getElementById("message"); |
8 | 14 | const loginBtn = document.getElementById("login-btn"); |
9 | 15 | const registerBtn = document.getElementById("register-btn"); |
| 16 | + const logoutBtn = document.getElementById("logout-btn"); |
| 17 | + const loginView = document.getElementById("login-view"); |
| 18 | + const userView = document.getElementById("user-view"); |
| 19 | + const userInfo = document.getElementById("user-info"); |
10 | 20 |
|
11 | | - loginBtn.addEventListener("click", () => { |
12 | | - const email = emailInput.value.trim(); |
13 | | - const password = passwordInput.value.trim(); |
14 | | - if (!email || !password) return showMessage("Please enter email and password.", "red"); |
15 | | - auth.signInWithEmailAndPassword(email, password) |
16 | | - .catch((error) => showMessage(`Login Error: ${error.message}`, "red")); |
| 21 | + // --- Attach Event Listeners --- |
| 22 | + loginBtn.addEventListener("click", () => handleAuth(false)); |
| 23 | + registerBtn.addEventListener("click", () => handleAuth(true)); |
| 24 | + logoutBtn.addEventListener("click", () => firebase.auth().signOut()); |
| 25 | + |
| 26 | + // --- Main Auth State Listener --- |
| 27 | + firebase.auth().onAuthStateChanged((user) => { |
| 28 | + if (user) { |
| 29 | + // User is signed in |
| 30 | + loginView.style.display = 'none'; |
| 31 | + userView.style.display = 'block'; |
| 32 | + userInfo.textContent = user.displayName || user.email; |
| 33 | + } else { |
| 34 | + // User is signed out |
| 35 | + loginView.style.display = 'block'; |
| 36 | + userView.style.display = 'none'; |
| 37 | + } |
17 | 38 | }); |
18 | 39 |
|
19 | | - registerBtn.addEventListener("click", () => { |
| 40 | + // --- New Compliant Authentication Handler --- |
| 41 | + async function handleAuth(isRegistering) { |
20 | 42 | const name = nameInput.value.trim(); |
21 | 43 | const email = emailInput.value.trim(); |
22 | 44 | const password = passwordInput.value.trim(); |
23 | | - if (!name || !email || !password) return showMessage("Please fill all fields.", "red"); |
24 | | - auth.createUserWithEmailAndPassword(email, password) |
25 | | - .then((cred) => cred.user.updateProfile({displayName: name})) |
26 | | - .catch((error) => showMessage(`Registration Error: ${error.message}`, "red")); |
27 | | - }); |
28 | 45 |
|
29 | | - auth.onAuthStateChanged((user) => { |
30 | | - if (user) { |
31 | | - showMessage(`Logged in as ${user.displayName || user.email}`, "green"); |
32 | | - chrome.storage.local.set({username: user.displayName}); |
33 | | - } else { |
34 | | - showMessage("Please login or register", "blue"); |
| 46 | + if (isRegistering && !name) return showMessage("Please enter a name to register.", "red"); |
| 47 | + if (!email || !password) return showMessage("Please enter both email and password.", "red"); |
| 48 | + |
| 49 | + try { |
| 50 | + showMessage("Connecting...", "grey"); |
| 51 | + const createTokenFunction = firebase.functions().httpsCallable("createCustomAuthToken"); |
| 52 | + |
| 53 | + const result = await createTokenFunction({ |
| 54 | + email, |
| 55 | + password, |
| 56 | + name, |
| 57 | + isRegistering, |
| 58 | + }); |
| 59 | + |
| 60 | + const token = result.data.token; |
| 61 | + if (token) { |
| 62 | + // Use the secure token to sign in on the client side |
| 63 | + await firebase.auth().signInWithCustomToken(token); |
| 64 | + showMessage("Success!", "green"); |
| 65 | + } else { |
| 66 | + throw new Error("Failed to get authentication token."); |
| 67 | + } |
| 68 | + |
| 69 | + } catch (error) { |
| 70 | + console.error("Authentication Error:", error); |
| 71 | + showMessage(`Error: ${error.message}`, "red"); |
35 | 72 | } |
36 | | - }); |
| 73 | + } |
37 | 74 |
|
38 | 75 | function showMessage(msg, color) { |
39 | 76 | messageDiv.textContent = msg; |
|
0 commit comments