Student Login
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, sendEmailVerification, signOut } from "firebase/auth";
import { getDatabase, ref, set, get } from "firebase/database";
const auth = getAuth();
const db = getDatabase();
// --- REGISTER FUNCTION ---
window.handleRegister = async () => {
const name = document.getElementById('reg-name').value;
const studentID = document.getElementById('reg-id').value;
const pass = document.getElementById('reg-pass').value;
const email = `${studentID}@htdc.com`; // Creates a unique auth email
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, pass);
const user = userCredential.user;
// 1. Send Free Verification Link
await sendEmailVerification(user);
alert("Account created! Please check your email for the verification link.");
// 2. Save Student Info to Database
await set(ref(db, 'students/' + studentID), {
fullName: name,
id: studentID,
verified: false,
joinedDate: new Date().toISOString()
});
toggleAuth(); // Switch to login view
} catch (error) {
alert(error.message);
}
};
// --- LOGIN FUNCTION ---
window.handleLogin = async () => {
const studentID = document.getElementById('login-id').value;
const pass = document.getElementById('login-pass').value;
const email = `${studentID}@htdc.com`;
try {
const userCredential = await signInWithEmailAndPassword(auth, email, pass);
const user = userCredential.user;
if (user.emailVerified) {
showDashboard(studentID);
} else {
alert("Please verify your email link before logging in.");
await signOut(auth);
}
} catch (error) {
alert("Invalid ID or Password");
}
};
// --- SHOW DASHBOARD ---
async function showDashboard(id) {
const snapshot = await get(ref(db, 'students/' + id));
if (snapshot.exists()) {
document.getElementById('student-name-display').innerText = snapshot.val().fullName;
document.getElementById('auth-section').style.display = 'none';
document.getElementById('dashboard-section').style.display = 'block';
// Load Results/Payments (Example)
document.getElementById('result-history').innerText = "HSC Exam Data Pending...";
}
}