Skip to content

Commit 534e87f

Browse files
committed
Enhance authentication logging and session management
- Added performance tracking for authentication and crazy games authorization processes. - Implemented session change notifications to improve reactivity in session state. - Cleaned up console logging for better clarity on authentication success and failure durations.
1 parent 2948487 commit 534e87f

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

components/auth/auth.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { inIframe } from "../utils/inIframe";
22
import { toast } from "react-toastify";
33
import retryManager from "../utils/retryFetch";
4+
import { useState, useEffect } from "react";
45

56
// secret: userDb.secret, username: userDb.username, email: userDb.email, staff: userDb.staff, canMakeClues: userDb.canMakeClues, supporter: userDb.supporter
67
let session = false;
78
// null = not logged in
89
// false = session loading/fetching
910

11+
// Listeners for session changes
12+
const sessionListeners = new Set();
13+
function notifySessionChange() {
14+
sessionListeners.forEach(listener => listener(session));
15+
}
16+
1017
export function signOut() {
1118
window.localStorage.removeItem("wg_secret");
1219
session = null;
20+
notifySessionChange();
1321
if(window.dontReconnect) {
1422
return;
1523
}
@@ -58,6 +66,18 @@ export function signIn() {
5866
}
5967

6068
export function useSession() {
69+
// sessionState is only used to trigger re-renders when session changes
70+
const [, setSessionState] = useState(session);
71+
72+
// Subscribe to session changes
73+
useEffect(() => {
74+
const listener = (newSession) => {
75+
setSessionState(newSession);
76+
};
77+
sessionListeners.add(listener);
78+
return () => sessionListeners.delete(listener);
79+
}, []);
80+
6181
if(typeof window === "undefined") {
6282
return {
6383
data: false
@@ -88,6 +108,7 @@ export function useSession() {
88108

89109
window.fetchingSession = true;
90110

111+
const authStartTime = performance.now();
91112
console.log(`[Auth] Starting authentication with retry mechanism (5s timeout, unlimited retries)`);
92113

93114
retryManager.fetchWithRetry(
@@ -110,26 +131,31 @@ export function useSession() {
110131
.then((res) => res.json())
111132
.then((data) => {
112133
window.fetchingSession = false;
113-
console.log(`[Auth] Authentication successful`);
134+
const authDuration = (performance.now() - authStartTime).toFixed(0);
135+
console.log(`[Auth] Authentication successful (took ${authDuration}ms)`);
114136

115137
if (data.error) {
116138
console.error(`[Auth] Server error:`, data.error);
117139
session = null;
140+
notifySessionChange();
118141
return;
119142
}
120143

121144
if (data.secret) {
122145
window.localStorage.setItem("wg_secret", data.secret);
123146
session = {token: data};
124147
console.log(`[Auth] Session established for user:`, data.username);
148+
notifySessionChange();
125149
} else {
126150
console.log(`[Auth] No session data received, user not logged in`);
127151
session = null;
152+
notifySessionChange();
128153
}
129154
})
130155
.catch((e) => {
131156
window.fetchingSession = false;
132-
console.error(`[Auth] Authentication failed:`, e.message);
157+
const authDuration = (performance.now() - authStartTime).toFixed(0);
158+
console.error(`[Auth] Authentication failed (took ${authDuration}ms):`, e.message);
133159

134160
// Clear potentially corrupted session data
135161
try {
@@ -139,9 +165,11 @@ export function useSession() {
139165
}
140166

141167
session = null;
168+
notifySessionChange();
142169
});
143170
} else {
144171
session = null;
172+
notifySessionChange();
145173
}
146174
}
147175

components/home.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ export default function Home({ }) {
245245
}
246246
}, [JSON.stringify(mainSession), inCrazyGames])
247247

248+
248249
// this breaks stuff like logout and set username reloads
249250
// useEffect(() => {
250251
// window.onbeforeunload = function(e) {
@@ -365,6 +366,7 @@ export default function Home({ }) {
365366
} catch (e) { }
366367
};
367368

369+
const crazyAuthStart = performance.now();
368370
fetch(clientConfigData.apiUrl + "/api/crazyAuth", {
369371
method: "POST",
370372
headers: {
@@ -376,7 +378,8 @@ export default function Home({ }) {
376378
callLoadingStop();
377379
return res.json();
378380
}).then((data) => {
379-
console.log("crazygames auth", token, user, data)
381+
const crazyAuthDuration = (performance.now() - crazyAuthStart).toFixed(0);
382+
console.log(`[CrazyAuth] completed (took ${crazyAuthDuration}ms)`, token, user, data)
380383
if (data.secret && data.username) {
381384
// Store full auth data including extended fields (elo, rank, etc.)
382385
setSession({ token: data })
@@ -398,7 +401,8 @@ export default function Home({ }) {
398401
}).catch((e) => {
399402
// Call loadingStop in case of network error (where first .then() never ran)
400403
callLoadingStop();
401-
console.error("crazygames auth failed", e)
404+
const crazyAuthDuration = (performance.now() - crazyAuthStart).toFixed(0);
405+
console.error(`[CrazyAuth] failed (took ${crazyAuthDuration}ms)`, e)
402406
});
403407

404408
}

0 commit comments

Comments
 (0)