mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: prevent useAuth infinite loop causing rate limit storms
checkAuth was defined as a plain function and used as a useEffect dependency, causing it to fire on every render. Moved it inside the effect with an empty dependency array so it runs once on mount.
This commit is contained in:
@@ -14,42 +14,42 @@ export function useAuth(): AuthState {
|
|||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
checkAuth();
|
async function checkAuth() {
|
||||||
}, [checkAuth]);
|
try {
|
||||||
|
// Check if auth is enabled
|
||||||
|
const configRes = await fetch("/api/v1/config/auth");
|
||||||
|
const config = await configRes.json();
|
||||||
|
|
||||||
async function checkAuth() {
|
if (!config.authEnabled) {
|
||||||
try {
|
setState({ loading: false, authEnabled: false, isAuthenticated: true });
|
||||||
// Check if auth is enabled
|
return;
|
||||||
const configRes = await fetch("/api/v1/config/auth");
|
}
|
||||||
const config = await configRes.json();
|
|
||||||
|
|
||||||
if (!config.authEnabled) {
|
// Auth is enabled — check if we have a valid session
|
||||||
|
const token = localStorage.getItem("stirling-token");
|
||||||
|
if (!token) {
|
||||||
|
setState({ loading: false, authEnabled: true, isAuthenticated: false });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sessionRes = await fetch("/api/auth/session", {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (sessionRes.ok) {
|
||||||
|
setState({ loading: false, authEnabled: true, isAuthenticated: true });
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem("stirling-token");
|
||||||
|
setState({ loading: false, authEnabled: true, isAuthenticated: false });
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Can't reach API — assume no auth needed (dev mode)
|
||||||
setState({ loading: false, authEnabled: false, isAuthenticated: true });
|
setState({ loading: false, authEnabled: false, isAuthenticated: true });
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth is enabled — check if we have a valid session
|
|
||||||
const token = localStorage.getItem("stirling-token");
|
|
||||||
if (!token) {
|
|
||||||
setState({ loading: false, authEnabled: true, isAuthenticated: false });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sessionRes = await fetch("/api/auth/session", {
|
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (sessionRes.ok) {
|
|
||||||
setState({ loading: false, authEnabled: true, isAuthenticated: true });
|
|
||||||
} else {
|
|
||||||
localStorage.removeItem("stirling-token");
|
|
||||||
setState({ loading: false, authEnabled: true, isAuthenticated: false });
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Can't reach API — assume no auth needed (dev mode)
|
|
||||||
setState({ loading: false, authEnabled: false, isAuthenticated: true });
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
checkAuth();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user