🎈
🌟
🎨
🧩
First Step School

First Step School

👩‍🏫

Teacher Portal

Administration Panel Login

import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js'; import { getAuth, signInWithEmailAndPassword, onAuthStateChanged, signOut, updateProfile } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js'; import { getFirestore, collection, addDoc, serverTimestamp, getDoc, doc, setDoc } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js'; const firebaseConfig = { apiKey: "AIzaSyAkOLt6QrnRYwEId2CCn0Zf9LA5jIfZUaw", authDomain: "first-step-school-adm456.firebaseapp.com", projectId: "first-step-school-adm456", storageBucket: "first-step-school-adm456.firebasestorage.app", messagingSenderId: "808201740631", appId: "1:808201740631:web:58acc0560b9069b4ffde12" }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getFirestore(app); const loginView = document.getElementById('teacher-login-view'); const dashboardView = document.getElementById('teacher-dashboard-view'); const loginForm = document.getElementById('teacher-login-form'); const loginError = document.getElementById('login-error'); const logoutBtn = document.getElementById('logout-btn'); // --- AUTH GUARD --- onAuthStateChanged(auth, async (user) => { if (user) { try { const idTokenResult = await user.getIdTokenResult(); const role = idTokenResult.claims.role || 'student'; if (role === 'teacher') { // If they are on the login page but already logged in, send to dashboard window.location.href = 'admin-dashboard.html'; } else if (role === 'principal') { window.location.href = 'principal-dashboard.html'; } else if (role === 'student') { window.location.href = 'student-dashboard.html'; } } catch (err) { console.error("Auth claim check failed:", err); } } else { loginView.classList.remove('hidden'); dashboardView.classList.add('hidden'); } }); if(loginForm) { loginForm.addEventListener('submit', async (e) => { e.preventDefault(); const name = document.getElementById('t-name').value; const email = document.getElementById('t-email').value; const password = document.getElementById('t-password').value; const selectedClass = document.getElementById('t-class').value; const btnSubmit = loginForm.querySelector('button[type="submit"]'); try { btnSubmit.disabled = true; btnSubmit.innerText = 'Verifying...'; const userCredential = await signInWithEmailAndPassword(auth, email, password); const user = userCredential.user; const idTokenResult = await user.getIdTokenResult(); const role = idTokenResult.claims.role; if (role !== 'teacher') { throw new Error("Unauthorized! This portal is for Teachers only."); } // --- PROFILE SYNC (Identity Sync) --- // 1. Update Auth Display Name await updateProfile(user, { displayName: name }); // 2. Update private staff profile await setDoc(doc(db, 'staff_profiles', user.uid), { name: name, email: email, className: selectedClass, lastLogin: new Date().toISOString() }, { merge: true }); // 3. Update public teachers directory (UID-based sync) const teacherPublicRef = doc(db, 'teachers', user.uid); await setDoc(teacherPublicRef, { name: name, className: selectedClass, subject: 'All Subjects', // Default; can be updated in dashboard email: email, updatedAt: serverTimestamp() }, { merge: true }); // Save session info sessionStorage.setItem('adminName', name || 'Teacher'); sessionStorage.setItem('adminClass', selectedClass); sessionStorage.setItem('userRole', 'teacher'); window.location.href = 'admin-dashboard.html'; } catch (error) { console.error('Login error:', error); loginError.innerText = error.message.includes('auth/') ? "Invalid email or password." : error.message; loginError.classList.remove('hidden'); btnSubmit.disabled = false; btnSubmit.innerText = 'Login'; } }); } if(logoutBtn) { logoutBtn.addEventListener('click', () => { signOut(auth).then(() => { sessionStorage.clear(); window.location.href = 'teacher-portal.html'; }); }); }