simple login/checkauth/main app components
This commit is contained in:
83
src/App.jsx
83
src/App.jsx
@@ -1,38 +1,63 @@
|
||||
import { useState } from 'react'
|
||||
import { useState, useEffect } from 'react'
|
||||
import reactLogo from './assets/react.svg'
|
||||
import viteLogo from '/vite.svg'
|
||||
import './App.css'
|
||||
|
||||
import Login from './components/login.jsx'
|
||||
import MainApp from './components/MainApp.jsx'
|
||||
|
||||
export default function App() {
|
||||
const [loading, setLoading] = useState(true); // true while checking auth
|
||||
const [loggedIn, setLoggedIn] = useState(false);
|
||||
|
||||
function App() {
|
||||
const [count, setCount] = useState(0)
|
||||
useEffect(() => {
|
||||
async function checkAuth() {
|
||||
try {
|
||||
// This endpoint should validate the refresh token cookie and return an access token
|
||||
const res = await fetch("http://localhost:7080/auth/login_refresh_token", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "react-app",
|
||||
},
|
||||
credentials: "include", // send cookies automatically
|
||||
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
|
||||
setLoggedIn(true);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Auth check failed:", err);
|
||||
setLoggedIn(false);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<a href="https://vite.dev" target="_blank">
|
||||
<img src={viteLogo} className="logo" alt="Vite logo" />
|
||||
</a>
|
||||
<a href="https://react.dev" target="_blank">
|
||||
<img src={reactLogo} className="logo react" alt="React logo" />
|
||||
</a>
|
||||
</div>
|
||||
<h1>Vite + React</h1>
|
||||
<div className="card">
|
||||
<button onClick={() => setCount((count) => count + 1)}>
|
||||
count is {count}
|
||||
</button>
|
||||
<p>
|
||||
Edit <code>src/App.jsx</code> and save to test HMR
|
||||
</p>
|
||||
</div>
|
||||
<p className="read-the-docs">
|
||||
Click on the Vite and React logos to learn more
|
||||
</p>
|
||||
<Login />
|
||||
</>
|
||||
)
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
if (loading) return <p>Loading...</p>; // can show a spinner
|
||||
|
||||
// Show main app if logged in, otherwise show login
|
||||
return loggedIn ? (
|
||||
<MainApp />
|
||||
) : (
|
||||
<Login onLogin={() => setLoggedIn(true)} />
|
||||
);
|
||||
}
|
||||
|
||||
export default App
|
||||
//export default App
|
||||
|
||||
Reference in New Issue
Block a user