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([]);
|
||||
|
||||
useEffect(() => {
|
||||
async function checkAuth() {
|
||||
try {
|
||||
// This endpoint should validate the refresh token cookie and return an access token
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
async function checkAuth() {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/auth/login_refresh_token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -35,33 +37,20 @@ export default function App() {
|
||||
}),
|
||||
});
|
||||
|
||||
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);
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Auth check failed:", err);
|
||||
setLoggedIn(false);
|
||||
|
||||
const data = await res.json();
|
||||
localStorage.setItem("access_tokens", JSON.stringify(data.tokens));
|
||||
setClientId(data.user_id);
|
||||
setLoggedIn(true);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
if (loading) return <p>Loading...</p>; // can show a spinner
|
||||
|
||||
@@ -69,7 +58,7 @@ export default function App() {
|
||||
return loggedIn ? (
|
||||
<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
|
||||
//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) {
|
||||
console.error(err);
|
||||
|
||||
Reference in New Issue
Block a user