simple login/checkauth/main app components

This commit is contained in:
2025-11-30 00:15:55 +01:00
parent c5241eb155
commit 61a7ab64d1
4 changed files with 96 additions and 52 deletions

View File

@@ -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 (
<div style={{ padding: "2rem" }}>
<h1>Welcome to MyHotel!</h1>
<p>Your access token (short-lived) is:</p>
<pre>{accessToken}</pre>
<section>
<h2>Dashboard</h2>
<ul>
<li>Rooms</li>
<li>Bookings</li>
<li>Guests</li>
<li>Reports</li>
</ul>
</section>
</div>
);
}

View File

@@ -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);

View File

@@ -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 (
<form onSubmit={handleSubmit} style={{ display: "flex", flexDirection: "column", width: "200px" }}>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Login</button>
{error && <p style={{ color: "red" }}>{error}</p>}
</form>
);
}
}