implement user authentication with login functionality and database integration
This commit is contained in:
@@ -1,41 +1,28 @@
|
||||
import "./App.css";
|
||||
import Layout from "./layout/Layout";
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import Form1 from "./components/Form1";
|
||||
import Form2 from "./components/Form2";
|
||||
import Form3 from "./components/Form3";
|
||||
import LoginForm from "./components/LoginForm";
|
||||
import Cookies from "js-cookie";
|
||||
import { ToastContainer } from "react-toastify";
|
||||
|
||||
function App() {
|
||||
const Items = [
|
||||
{
|
||||
id: 1,
|
||||
title: "Mock Book 1",
|
||||
author: "Author 1",
|
||||
description: "Description for Mock Book 1",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "Mock Book 2",
|
||||
author: "Author 2",
|
||||
description: "Description for Mock Book 2",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: "Mock Book 3",
|
||||
author: "Author 3",
|
||||
description: "Description for Mock Book 3",
|
||||
},
|
||||
];
|
||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem("allItems", JSON.stringify(Items));
|
||||
localStorage.setItem("borrowableItems", JSON.stringify(Items));
|
||||
if (Cookies.get("token")) {
|
||||
setIsLoggedIn(true);
|
||||
}
|
||||
|
||||
localStorage.setItem("borrowableItems", JSON.stringify([]));
|
||||
localStorage.setItem("borrowCode", "123456");
|
||||
}, []);
|
||||
|
||||
return (
|
||||
// Mock flow without real logic: show the three sections stacked for design preview
|
||||
return isLoggedIn ? (
|
||||
<Layout>
|
||||
{/* Mock flow without real logic: show the three sections stacked for design preview */}
|
||||
<div className="space-y-10">
|
||||
<Form1 />
|
||||
<div className="h-px bg-blue-100" />
|
||||
@@ -44,6 +31,23 @@ function App() {
|
||||
<Form3 />
|
||||
</div>
|
||||
</Layout>
|
||||
) : (
|
||||
<>
|
||||
<LoginForm onLogin={() => setIsLoggedIn(true)} />
|
||||
<ToastContainer
|
||||
position="top-right"
|
||||
autoClose={3000}
|
||||
hideProgressBar={false}
|
||||
newestOnTop
|
||||
closeOnClick
|
||||
rtl={false}
|
||||
pauseOnFocusLoss={false}
|
||||
draggable
|
||||
pauseOnHover
|
||||
theme="light"
|
||||
className="!z-50"
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -1,13 +1,6 @@
|
||||
import React from "react";
|
||||
import { myToast } from "../utils/toastify";
|
||||
|
||||
const Form3: React.FC = () => {
|
||||
if (localStorage.getItem("borrowCode")) {
|
||||
myToast("Ausleihe erfolgreich!", "success");
|
||||
} else {
|
||||
myToast("Ausleihe fehlgeschlagen!", "error");
|
||||
}
|
||||
|
||||
const code = localStorage.getItem("borrowCode") ?? "—";
|
||||
|
||||
return (
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import React from "react";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
const Header: React.FC = () => {
|
||||
return (
|
||||
@@ -9,6 +10,15 @@ const Header: React.FC = () => {
|
||||
<p className="text-blue-500 mt-1 md:mt-2 text-base md:text-lg font-medium">
|
||||
Schnell und unkompliziert Equipment reservieren
|
||||
</p>
|
||||
<button
|
||||
onClick={() => {
|
||||
Cookies.remove("token");
|
||||
window.location.reload();
|
||||
}}
|
||||
className="text-blue-500 hover:underline"
|
||||
>
|
||||
Logout
|
||||
</button>
|
||||
</header>
|
||||
);
|
||||
};
|
||||
|
74
frontend/src/components/LoginForm.tsx
Normal file
74
frontend/src/components/LoginForm.tsx
Normal file
@@ -0,0 +1,74 @@
|
||||
import React from "react";
|
||||
import { useState } from "react";
|
||||
import { loginUser } from "../utils/fetchData";
|
||||
import { myToast } from "../utils/toastify";
|
||||
|
||||
type LoginFormProps = {
|
||||
onLogin: () => void;
|
||||
};
|
||||
|
||||
const LoginForm: React.FC<LoginFormProps> = ({ onLogin }) => {
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
const result = await loginUser(username, password);
|
||||
if (result.success) {
|
||||
myToast("Login successful!", "success");
|
||||
onLogin();
|
||||
} else {
|
||||
myToast("Login failed. Please check your credentials.", "error");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="bg-blue-950 min-h-screen">
|
||||
<div className="max-w-sm mx-auto mt-20 bg-white rounded-xl shadow-lg p-8 border border-blue-100">
|
||||
<h2 className="text-2xl font-bold text-blue-700 mb-6 text-center">
|
||||
Login
|
||||
</h2>
|
||||
<form onSubmit={handleSubmit} className="space-y-5">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="username"
|
||||
className="block text-sm font-medium text-gray-700 mb-1"
|
||||
>
|
||||
Username
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
id="username"
|
||||
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 px-3 py-2"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label
|
||||
htmlFor="password"
|
||||
className="block text-sm font-medium text-gray-700 mb-1"
|
||||
>
|
||||
Password
|
||||
</label>
|
||||
<input
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
type="password"
|
||||
id="password"
|
||||
className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500 px-3 py-2"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
className="w-full bg-blue-600 text-white font-bold py-2 px-4 rounded-md shadow hover:bg-blue-700 transition"
|
||||
>
|
||||
Login
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default LoginForm;
|
@@ -1,7 +1,10 @@
|
||||
import React from "react";
|
||||
import Object from "./Object";
|
||||
import { useEffect } from "react";
|
||||
|
||||
const Sidebar: React.FC = () => {
|
||||
useEffect(() => {});
|
||||
|
||||
const items = JSON.parse(localStorage.getItem("allItems") || "[]");
|
||||
|
||||
return (
|
||||
@@ -20,16 +23,16 @@ const Sidebar: React.FC = () => {
|
||||
d="M16.5 7.5V4.75A2.25 2.25 0 0 0 14.25 2.5h-4.5A2.25 2.25 0 0 0 7.5 4.75V7.5m9 0h-9m9 0v11.75A2.25 2.25 0 0 1 14.25 21.5h-4.5A2.25 2.25 0 0 1 7.5 19.25V7.5m9 0h-9"
|
||||
/>
|
||||
</svg>
|
||||
Ausleih-Übersicht
|
||||
Geräte Übersicht
|
||||
</h2>
|
||||
|
||||
<div className="space-y-4 flex-1 overflow-auto pr-1">
|
||||
{items.map((item: any) => (
|
||||
<div
|
||||
key={item.id}
|
||||
key={item.item_name}
|
||||
className="bg-white/80 rounded-xl p-4 shadow hover:shadow-md transition"
|
||||
>
|
||||
<Object title={item.title} description={item.description} />
|
||||
<Object title={item.item_name} description={"test"} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
@@ -0,0 +1,53 @@
|
||||
import Cookies from "js-cookie";
|
||||
import { myToast } from "./toastify";
|
||||
|
||||
export const fetchAllData = async (token: string | undefined) => {
|
||||
if (!token) return;
|
||||
try {
|
||||
// First we fetch all items that are potentially available for borrowing
|
||||
const response = await fetch("http://localhost:8002/api/items", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
myToast("Failed to fetch items", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
localStorage.setItem("allItems", JSON.stringify(data));
|
||||
} catch (error) {
|
||||
myToast("An error occurred", "error");
|
||||
}
|
||||
};
|
||||
|
||||
export const loginUser = async (username: string, password: string) => {
|
||||
try {
|
||||
const response = await fetch("http://localhost:8002/api/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return { success: false } as const;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (data?.token) {
|
||||
Cookies.set("token", data.token);
|
||||
myToast("Login successful!", "success");
|
||||
fetchAllData(Cookies.get("token"));
|
||||
return { success: true, token: data.token } as const;
|
||||
}
|
||||
|
||||
return { success: false } as const;
|
||||
} catch (e) {
|
||||
return { success: false } as const;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user