diff --git a/src/App.jsx b/src/App.jsx index 43ff713..97f2d4d 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -19,11 +19,13 @@ export default function App() { const [clientId, setClientId] = useState([]); useEffect(() => { - async function checkAuth() { - try { - // This endpoint should validate the refresh token cookie and return an access token + checkAuth(); + }, []); - const res = await fetch(`${API_BASE}/auth/login_refresh_token`, { +async function checkAuth() { + setLoading(true); + try { + const res = await fetch(`${API_BASE}/auth/login_refresh_token`, { method: "POST", headers: { "Content-Type": "application/json", @@ -33,35 +35,22 @@ export default function App() { body: JSON.stringify({ device_id : "147ac10b-58cc-4372-a567-0e02b2c3d479", // or persistent device ID }), - }); + }); - console.log("sent checkAuth request") - - if (!res.ok) { - console.log(res) - console.log("checkAuth request not ok") - // No valid refresh token → user must log in - setLoggedIn(false); - } else { - // Got short-lived access token - console.log("Got short-lived access token") - const data = await res.json(); - console.log(data) - localStorage.setItem("access_tokens", JSON.stringify(data.tokens)); // store for API calls - setClientId(data.user_id) - - setLoggedIn(true); - } - } catch (err) { - console.error("Auth check failed:", err); - setLoggedIn(false); - } finally { - setLoading(false); - } + if (!res.ok) { + setLoggedIn(false); + return; } - checkAuth(); - }, []); + const data = await res.json(); + localStorage.setItem("access_tokens", JSON.stringify(data.tokens)); + setClientId(data.user_id); + setLoggedIn(true); + } finally { + setLoading(false); + } +} + if (loading) return
Loading...
; // can show a spinner @@ -69,7 +58,7 @@ export default function App() { return loggedIn ? (