// ========================= // IMPORTS DO FIREBASE // ========================= import { initializeApp } from "https://www.gstatic.com/firebasejs/11.0.1/firebase-app.js"; import { getAuth, onAuthStateChanged, signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/11.0.1/firebase-auth.js"; // ========================= // CONFIGURAÇÃO DO FIREBASE // (BRINCAEDU) // ========================= const firebaseConfig = { apiKey: "AIzaSyCqdpg0tHn8gqWZTVa3XPqu3a1GLbvpjXA", authDomain: "brincaedu-c9b17.firebaseapp.com", projectId: "brincaedu-c9b17", storageBucket: "brincaedu-c9b17.appspot.com", messagingSenderId: "75318397570", appId: "1:75318397570:web:14ca579ce0c87b7c85c29b", measurementId: "G-6REXCGXNPJ" }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const googleProvider = new GoogleAuthProvider(); // ========================= // ELEMENTOS DA TELA // ========================= const tabLogin = document.getElementById("tabLogin"); const tabRegister = document.getElementById("tabRegister"); const loginFormWrapper = document.getElementById("loginFormWrapper"); const registerFormWrapper = document.getElementById("registerFormWrapper"); const loginEmail = document.getElementById("loginEmail"); const loginPassword = document.getElementById("loginPassword"); const registerEmail = document.getElementById("registerEmail"); const registerPassword = document.getElementById("registerPassword"); const loginBtn = document.getElementById("loginBtn"); const registerBtn = document.getElementById("registerBtn"); const googleBtn = document.getElementById("googleBtn"); const authSection = document.getElementById("authSection"); const dashboardSection = document.getElementById("dashboardSection"); const logoutBtn = document.getElementById("logoutBtn"); const userInfo = document.getElementById("userInfo"); const authMessage = document.getElementById("authMessage"); // ========================= // FUNÇÃO AUXILIAR // ========================= function setMessage(text, type = "info") { authMessage.textContent = text; authMessage.className = "feedback " + type; if (text) { setTimeout(() => { authMessage.textContent = ""; authMessage.className = "feedback"; }, 6000); } } // ========================= // TROCA ENTRE LOGIN / REGISTRO // ========================= tabLogin.addEventListener("click", () => { tabLogin.classList.add("active"); tabRegister.classList.remove("active"); loginFormWrapper.style.display = "block"; registerFormWrapper.style.display = "none"; }); tabRegister.addEventListener("click", () => { tabRegister.classList.add("active"); tabLogin.classList.remove("active"); loginFormWrapper.style.display = "none"; registerFormWrapper.style.display = "block"; }); // ========================= // LOGIN COM E-MAIL / SENHA // ========================= loginBtn.addEventListener("click", async () => { const email = loginEmail.value.trim(); const password = loginPassword.value.trim(); if (!email || !password) { setMessage("Preencha e-mail e senha.", "error"); return; } try { setMessage("Entrando...", "info"); await signInWithEmailAndPassword(auth, email, password); setMessage(""); } catch (err) { setMessage("Erro ao entrar: " + err.message, "error"); } }); // ========================= // REGISTRO COM E-MAIL / SENHA // ========================= registerBtn.addEventListener("click", async () => { const email = registerEmail.value.trim(); const password = registerPassword.value.trim(); if (!email || !password) { setMessage("Preencha e-mail e senha para registrar.", "error"); return; } try { setMessage("Criando conta...", "info"); await createUserWithEmailAndPassword(auth, email, password); setMessage("Conta criada! Você já está logado.", "success"); } catch (err) { setMessage("Erro ao registrar: " + err.message, "error"); } }); // ========================= // LOGIN COM GOOGLE // ========================= googleBtn.addEventListener("click", async () => { try { setMessage("Conectando com Google...", "info"); await signInWithPopup(auth, googleProvider); setMessage(""); } catch (err) { setMessage("Erro ao entrar com Google: " + err.message, "error"); } }); // ========================= // ALTERA INTERFACE QUANDO LOGA // ========================= onAuthStateChanged(auth, (user) => { if (user) { authSection.style.display = "none"; dashboardSection.style.display = "block"; logoutBtn.style.display = "inline-flex"; userInfo.textContent = `Logado como: ${user.email || "Usuário sem e-mail"}`; } else { authSection.style.display = "block"; dashboardSection.style.display = "none"; logoutBtn.style.display = "none"; loginPassword.value = ""; registerPassword.value = ""; } }); // ========================= // LOGOUT // ========================= logoutBtn.addEventListener("click", async () => { await signOut(auth); });