// auth.jsx — Student Login / Register + Dashboard (wired to the WordPress REST API) (function () { const { useState, useEffect } = React; const Icon = window.Icon; const { PageHero } = window; const CFG = window.INFOTECH || {}; const STATUS_META = { valid: { cls: "status-valid", label: "Valid", color: "var(--ok)", bg: "var(--ok-bg)" }, expired: { cls: "status-expired", label: "Expired", color: "var(--warn)", bg: "var(--warn-bg)" }, cancelled: { cls: "status-cancelled", label: "Cancelled", color: "var(--bad)", bg: "var(--bad-bg)" }, }; /* ===================== LOGIN / REGISTER ===================== */ function Login({ navigate }) { const [mode, setMode] = useState("login"); // login | register const [form, setForm] = useState({ username: "", password: "", name: "", email: "", reg: "" }); const [errors, setErrors] = useState({}); const [msg, setMsg] = useState(""); const [busy, setBusy] = useState(false); // Already signed in? Go straight to the dashboard. useEffect(() => { if (CFG.loggedIn) navigate("dashboard"); /* eslint-disable-next-line */ }, []); const set = (k, v) => { setForm((f) => ({ ...f, [k]: v })); if (errors[k]) setErrors((e) => ({ ...e, [k]: null })); setMsg(""); }; const finishAuth = (data) => { if (data.nonce) window.setNonce(data.nonce); CFG.loggedIn = true; CFG.user = data.user; navigate("dashboard"); }; const submit = (e) => { e.preventDefault(); setErrors({}); setMsg(""); if (mode === "login") { if (!form.username.trim() || !form.password) { setMsg("Please enter your username/email and password."); return; } setBusy(true); window.api("login", { method: "POST", body: { username: form.username, password: form.password } }).then((res) => { setBusy(false); if (res.data && res.data.ok) finishAuth(res.data); else setMsg((res.data && res.data.message) || "Login failed. Please try again."); }); } else { setBusy(true); window.api("register", { method: "POST", body: { name: form.name, email: form.email, reg: form.reg, password: form.password } }).then((res) => { setBusy(false); if (res.data && res.data.ok) finishAuth(res.data); else if (res.data && res.data.errors) setErrors(res.data.errors); else setMsg((res.data && res.data.message) || "Could not create your account. Please try again."); }); } }; const Tab = ({ id, label }) => ( ); return (
{msg && (
{msg}
)}
{mode === "register" && (
set("name", e.target.value)} placeholder="Your name" /> {errors.name && {errors.name}}
set("email", e.target.value)} placeholder="you@email.com" /> {errors.email && {errors.email}}
set("reg", e.target.value)} placeholder="e.g. INF-23-1142" />
)} {mode === "login" && (
set("username", e.target.value)} placeholder="Username or email" autoComplete="username" />
)}
set("password", e.target.value)} placeholder="••••••••" autoComplete={mode === "login" ? "current-password" : "new-password"} /> {errors.password && {errors.password}}

{mode === "login" ? "New student?" : "Already have an account?"}{" "}

); } /* ===================== DASHBOARD ===================== */ function CertItem({ c }) { const meta = STATUS_META[c.status] || STATUS_META.valid; const downloadCert = () => { if (c.certFileUrl) window.open(c.certFileUrl, "_blank"); else window.makeCertPDF(c); }; return (

{c.course}

{c.certId}
{meta.label}
{[["Registration #", c.reg], ["Grade", c.grade], ["Completed", c.completion], ["Issued", c.issued]].map(([l, v]) => (
{l}
{v || "—"}
))}
{c.marks && c.marks.length > 0 && (
View result marks
{c.marks.map((m, i) => ( ))}
Subject Obtained Total
{m.subject} {m.obtained} {m.total}
)}
{c.status !== "cancelled" && ( )} {c.hasResult && ( Result / Marksheet )}
); } function Dashboard({ navigate }) { const [status, setStatus] = useState("loading"); // loading | ready | unauth const [data, setData] = useState(null); useEffect(() => { window.api("me").then((res) => { if (res.ok && res.data && res.data.ok) { setData(res.data); setStatus("ready"); } else { setStatus("unauth"); } }); }, []); const logout = () => { window.api("logout", { method: "POST" }).then((res) => { if (res.data && res.data.nonce) window.setNonce(res.data.nonce); CFG.loggedIn = false; CFG.user = null; navigate("home"); }); }; if (status === "loading") { return (

Loading your dashboard…

); } if (status === "unauth") { return (

Please sign in

You need to be signed in to view your student dashboard.

); } const u = data.user; const certs = data.certs || []; return (
{/* profile strip */}
{(u.name || "S").charAt(0).toUpperCase()}
{u.name}
{u.email}{u.reg ? " · Reg " + u.reg : ""}
{u.admin && ( Admin panel )}

My certificates & results

{certs.length} record{certs.length === 1 ? "" : "s"}
{certs.length === 0 ? (

No records yet

Your certificates and results will appear here once the institute issues them. If you have a Registration number, add it to your profile so we can link your records.

) : (
{certs.map((c) => )}
)}
); } Object.assign(window, { Login, Dashboard }); })();