diff --git a/src/App.jsx b/src/App.jsx index 5a40949..1a2bca7 100644 --- a/src/App.jsx +++ b/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 ( - <> -
- - Vite logo - - - React logo - -
-

Vite + React

-
- -

- Edit src/App.jsx and save to test HMR -

-
-

- Click on the Vite and React logos to learn more -

- - - ) + checkAuth(); + }, []); + + if (loading) return

Loading...

; // can show a spinner + + // Show main app if logged in, otherwise show login + return loggedIn ? ( + + ) : ( + setLoggedIn(true)} /> + ); } -export default App +//export default App diff --git a/src/components/MainApp.jsx b/src/components/MainApp.jsx new file mode 100644 index 0000000..4f37b9c --- /dev/null +++ b/src/components/MainApp.jsx @@ -0,0 +1,24 @@ + + +export default function MainApp() { + // You could fetch your short-lived access token from localStorage + const accessToken = localStorage.getItem("access_tokens"); + console.log("the accesToken in Mainapp is :" + accessToken); + return ( +
+

Welcome to MyHotel!

+

Your access token (short-lived) is:

+
{accessToken}
+ +
+

Dashboard

+
    +
  • Rooms
  • +
  • Bookings
  • +
  • Guests
  • +
  • Reports
  • +
+
+
+ ); +} diff --git a/src/components/auth_checking.jsx b/src/components/auth_checking.jsx index 09ad2f5..95c77e1 100644 --- a/src/components/auth_checking.jsx +++ b/src/components/auth_checking.jsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import Login from ".components/Login"; +import Login from "./components/Login"; import MainApp from "./MainApp"; export default function App() { @@ -29,7 +29,7 @@ export default function App() { const data = await res.json(); // store short-lived token somewhere accessible - localStorage.setItem("access_token", data.access_token); + localStorage.setItem("access_token", JSON.stringify(data.tokens)); setLoggedIn(true); } catch (err) { console.error("Auth check failed:", err); diff --git a/src/components/login.jsx b/src/components/login.jsx index 0d39212..a465e5b 100644 --- a/src/components/login.jsx +++ b/src/components/login.jsx @@ -1,29 +1,25 @@ import { useState } from "react"; -export default function Login() { - const [username, setUsername] = useState("") - const [password, setPassword] = useState("") - const [error, setError] = useState("") +export default function Login({ onLogin }) { + const [username, setUsername] = useState(""); + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); - async function handleSubmit(e) { + async function handleSubmit(e) { e.preventDefault(); setError(""); - const device_id = crypto.randomUUID(); // browser-native UUID + const device_id = "147ac10b-58cc-4372-a567-0e02b2c3d479" ; try { const res = await fetch("http://localhost:7080/auth/create_refresh", { method: "POST", headers: { "Content-Type": "application/json", - "User-Agent": "react-app" // optional + "User-Agent": "react-app", }, - credentials: "include", // lets cookies like PHPSESSID flow if server sets them - body: JSON.stringify({ - username, - password, - device_id - }), + credentials: "include", + body: JSON.stringify({ username, password, device_id }), }); if (!res.ok) { @@ -31,34 +27,33 @@ export default function Login() { return; } - const data = await res.text(); - console.log("Login success:", data); - // store tokens/responses where you want (context/localStorage/etc.) + //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 } catch (err) { + console.error(err); setError("Network error."); } } return ( -
+ setUsername(e.target.value)} /> - setPassword(e.target.value)} /> - - {error &&

{error}

}
); -} \ No newline at end of file +}