Skip to content

Commit c48ccfd

Browse files
refactor: standardize module loading by converting common.js to ES module
1 parent 0e86196 commit c48ccfd

13 files changed

Lines changed: 40 additions & 101 deletions

public/ai.html

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ <h3>🔍 Investigate:</h3>
3636
</div>
3737
</div>
3838

39-
<script type="module">
40-
import { auth } from "./js/firebase.js";
41-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
42-
import { guardMission } from "./js/guard.js";
43-
onAuthStateChanged(auth, async (user) => {
44-
if (!user) { window.location.href = "login.html"; return; }
45-
await guardMission('ai');
46-
});
47-
</script>
39+
<script type="module" src="js/auth.js"></script>
4840
<script src="js/common.js"></script>
4941
<script type="module" src="js/ai.js"></script>
5042
</body>

public/creator.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ <h3>Exported Mission:</h3>
5454
</div>
5555
</div>
5656

57-
<script type="module">
58-
import { auth } from "./js/firebase.js";
59-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
60-
onAuthStateChanged(auth, (user) => { if (!user) window.location.href = "login.html"; });
61-
</script>
57+
<script type="module" src="js/auth.js"></script>
6258
<script src="js/common.js"></script>
6359
<script type="module" src="js/creator.js"></script>
6460
</body>

public/daily.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ <h2 style="font-family:var(--font-cyber);color:var(--neon-cyan);font-size:16px;"
134134
</div>
135135
</div>
136136

137+
<script type="module" src="js/auth.js"></script>
137138
<script type="module">
138139
import { auth } from "./js/firebase.js";
139140
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
@@ -146,7 +147,7 @@ <h2 style="font-family:var(--font-cyber);color:var(--neon-cyan);font-size:16px;"
146147
let timerMax = 30;
147148

148149
onAuthStateChanged(auth, async (user) => {
149-
if (!user) { window.location.href = "login.html"; return; }
150+
if (!user) return;
150151
currentUser = user;
151152
await init();
152153
});

public/darkweb.html

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@ <h3>Risk Assessment</h3>
3232
</div>
3333
</div>
3434

35-
<script type="module">
36-
import { auth } from "./js/firebase.js";
37-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
38-
import { guardMission } from "./js/guard.js";
39-
onAuthStateChanged(auth, async (user) => {
40-
if (!user) { window.location.href = "login.html"; return; }
41-
await guardMission('darkweb');
42-
});
43-
</script>
35+
<script type="module" src="js/auth.js"></script>
4436
<script src="js/common.js"></script>
4537
<script type="module" src="js/darkweb.js"></script>
4638
</body>

public/hub.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ <h3 class="section-title">&#x1F6E0;&#xFE0F; Tools &amp; Knowledge</h3>
228228
if (userData) {
229229
const xp = userData.xp || 0;
230230
applyLockStates(xp);
231-
// Store new unlocks into notification bell (never show popup)
232-
const seenUnlocks = JSON.parse(localStorage.getItem('seenUnlocks') || '[]');
233-
const newItems = getNewlyUnlocked(0, xp).filter(u => !seenUnlocks.includes(u.id));
231+
const lastXP = parseInt(localStorage.getItem('lastSeenXP') || '0', 10);
232+
const newItems = getNewlyUnlocked(lastXP, xp).filter(u => !seenUnlocks.includes(u.id));
233+
localStorage.setItem('lastSeenXP', xp);
234234
newItems.forEach(u => addUnlockNotification(u));
235235
if (newItems.length) {
236+
const seenUnlocks = JSON.parse(localStorage.getItem('seenUnlocks') || '[]');
236237
localStorage.setItem('seenUnlocks', JSON.stringify([...seenUnlocks, ...newItems.map(u => u.id)]));
237238
}
238239
updateBell();

public/incident.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,12 @@ <h1>🚨 Incident Response Simulator</h1>
150150
<div id="missionArea"></div>
151151
</div>
152152

153+
<script type="module" src="js/auth.js"></script>
153154
<script type="module">
154155
import { auth } from "./js/firebase.js";
155156
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
156157
import { updateXP, getUserData } from "./js/xp.js";
157158
import { showSafetySteps } from "./js/adaptive.js";
158-
import { guardMission } from "./js/guard.js";
159-
160-
onAuthStateChanged(auth, async (user) => {
161-
if (!user) { window.location.href = "login.html"; return; }
162-
await guardMission('incident');
163-
const data = await getUserData();
164-
const xp = data?.xp || 0;
165-
document.getElementById("xpDisplay").innerText = `⚡ ${xp} XP`;
166-
startSimulator();
167-
});
168159

169160
// ── SCENARIO TREE ──────────────────────────────────────────
170161
// Each node: { id, tag, title, body, choices: [{text, next, damage, consequence, good}] }
@@ -418,7 +409,12 @@ <h2>${isGood ? "🛡️" : isPartial ? "⚠️" : "💀"} ${node.title}</h2>
418409
updateDots();
419410
};
420411

421-
startSimulator();
412+
onAuthStateChanged(auth, async (user) => {
413+
if (!user) return;
414+
const data = await getUserData();
415+
document.getElementById("xpDisplay").innerText = `⚡ ${data?.xp || 0} XP`;
416+
startSimulator();
417+
});
422418
</script>
423419
<script src="js/common.js"></script>
424420
</body>

public/js/auth.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { auth } from "./firebase.js";
22
import { signOut, GoogleAuthProvider, signInWithPopup, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
3+
import { guardMission } from "./guard.js";
4+
5+
const path = window.location.pathname.split('/').pop() || 'index.html';
6+
const isLoginPage = path === 'index.html' || path === '';
7+
const guardedMissions = { 'ai.html': 'ai', 'darkweb.html': 'darkweb' };
38

49
function showError(msg) {
510
const el = document.getElementById("auth-error");
@@ -29,13 +34,18 @@ window.logout = async function () {
2934

3035
document.getElementById("googleBtn")?.addEventListener("click", googleLogin);
3136

32-
onAuthStateChanged(auth, (user) => {
33-
if (user) {
34-
if (window.location.pathname.endsWith("index.html")) {
35-
window.location.href = "hub.html";
36-
return;
37-
}
38-
const el = document.getElementById("userEmail");
39-
if (el) el.innerText = `Hi, ${user.displayName || user.email.split('@')[0]}!`;
37+
onAuthStateChanged(auth, async (user) => {
38+
if (!user) {
39+
if (!isLoginPage) window.location.href = "index.html";
40+
return;
41+
}
42+
if (isLoginPage) {
43+
window.location.href = "hub.html";
44+
return;
4045
}
46+
const el = document.getElementById("userEmail");
47+
if (el) el.innerText = `Hi, ${user.displayName || user.email.split('@')[0]}!`;
48+
49+
const missionId = guardedMissions[path];
50+
if (missionId) await guardMission(missionId);
4151
});

public/malware.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ <h3 id="healthText">System Health: 100</h3>
3838
<div id="options" style="display:none;"></div>
3939
</div>
4040

41-
<script type="module">
42-
import { auth } from "./js/firebase.js";
43-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
44-
45-
onAuthStateChanged(auth, (user) => {
46-
if (!user) {
47-
window.location.href = "login.html";
48-
}
49-
});
50-
</script>
41+
<script type="module" src="js/auth.js"></script>
5142
<script src="js/common.js"></script>
5243
<script type="module" src="js/malware.js"></script>
5344
</body>

public/password.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,7 @@ <h3 style="color: var(--neon-magenta);">🚨 MFA Push Notification</h3>
6666
</div>
6767
</div>
6868

69-
<script type="module">
70-
import { auth } from "./js/firebase.js";
71-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
72-
73-
onAuthStateChanged(auth, (user) => {
74-
if (!user) {
75-
window.location.href = "login.html";
76-
}
77-
});
78-
</script>
69+
<script type="module" src="js/auth.js"></script>
7970
<script src="js/common.js"></script>
8071
<script type="module" src="js/password.js"></script>
8172
</body>

public/phishing.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,7 @@ <h3>🔐 Login Required</h3>
6363
<button id="nextBtn" class="primary-btn" style="display: none">Next</button>
6464
</div>
6565

66-
<script type="module">
67-
import { auth } from "./js/firebase.js";
68-
import { onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.12.2/firebase-auth.js";
69-
70-
onAuthStateChanged(auth, (user) => {
71-
if (!user) {
72-
window.location.href = "login.html";
73-
}
74-
});
75-
</script>
66+
<script type="module" src="js/auth.js"></script>
7667
<script src="js/common.js"></script>
7768
<script type="module" src="js/phishing.js"></script>
7869
</body>

0 commit comments

Comments
 (0)