register user fix + login fix
This commit is contained in:
37
src/App.jsx
37
src/App.jsx
@@ -19,10 +19,12 @@ 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
|
|
||||||
|
|
||||||
|
async function checkAuth() {
|
||||||
|
setLoading(true);
|
||||||
|
try {
|
||||||
const res = await fetch(`${API_BASE}/auth/login_refresh_token`, {
|
const res = await fetch(`${API_BASE}/auth/login_refresh_token`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
@@ -35,33 +37,20 @@ export default function App() {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("sent checkAuth request")
|
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
console.log(res)
|
|
||||||
console.log("checkAuth request not ok")
|
|
||||||
// No valid refresh token → user must log in
|
|
||||||
setLoggedIn(false);
|
setLoggedIn(false);
|
||||||
} else {
|
return;
|
||||||
// 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);
|
const data = await res.json();
|
||||||
setLoggedIn(false);
|
localStorage.setItem("access_tokens", JSON.stringify(data.tokens));
|
||||||
|
setClientId(data.user_id);
|
||||||
|
setLoggedIn(true);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkAuth();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
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} />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user