register user fix + login fix

This commit is contained in:
2026-01-10 13:44:49 +01:00
parent eef0c35134
commit cd459a1aa0
2 changed files with 21 additions and 32 deletions

View File

@@ -19,11 +19,13 @@ export default function App() {
const [clientId, setClientId] = useState([]); const [clientId, setClientId] = useState([]);
useEffect(() => { useEffect(() => {
async function checkAuth() { checkAuth();
try { }, []);
// This endpoint should validate the refresh token cookie and return an access token
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", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -33,35 +35,22 @@ export default function App() {
body: JSON.stringify({ body: JSON.stringify({
device_id : "147ac10b-58cc-4372-a567-0e02b2c3d479", // or persistent device ID device_id : "147ac10b-58cc-4372-a567-0e02b2c3d479", // or persistent device ID
}), }),
}); });
console.log("sent checkAuth request") if (!res.ok) {
setLoggedIn(false);
if (!res.ok) { return;
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);
}
} }
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 <p>Loading...</p>; // can show a spinner if (loading) return <p>Loading...</p>; // can show a spinner
@@ -69,7 +58,7 @@ export default function App() {
return loggedIn ? ( return loggedIn ? (
<MainApp resClientId ={clientId} /> <MainApp resClientId ={clientId} />
) : ( ) : (
<Login onLogin={() => setLoggedIn(true)} /> <Login onLogin={checkAuth} />
); );
} }

View File

@@ -32,7 +32,7 @@ export default function Login({ onLogin }) {
//const data = await res.json(); // server returns short-lived token //const data = await res.json(); // server returns short-lived token
//localStorage.setItem("access_token", data.access_token); //localStorage.setItem("access_token", data.access_token);
if (onLogin) onLogin(); // notify App to show MainApp if (onLogin) await onLogin(); // notify App to show MainApp
} catch (err) { } catch (err) {
console.error(err); console.error(err);