added with bugs

This commit is contained in:
2025-07-25 10:50:00 +02:00
parent a80720fe5f
commit 93d535dad9
4 changed files with 63 additions and 47 deletions

View File

@@ -1,28 +1,47 @@
import "./App.css"; import "./App.css";
import Layout from "./layout/Layout"; import Layout from "./layout/Layout";
import UserTable from "./components/UserTable"; import UserTable from "./components/UserTable";
import { useContext, useEffect } from "react"; import LoginCard from "./components/LoginCard";
import { useEffect, useState } from "react";
import { loadTheme } from "./utils/frontendService"; import { loadTheme } from "./utils/frontendService";
import "react-toastify/dist/ReactToastify.css"; import "react-toastify/dist/ReactToastify.css";
import { AuthContext } from "./utils/context"; import Cookies from "js-cookie";
function App() { function App() {
useEffect(() => { useEffect(() => {
loadTheme(); loadTheme();
}, []); }, []);
const auth = useContext(AuthContext); // Track authentication state
const [isAuthenticated, setIsAuthenticated] = useState(
!!Cookies.get("token")
);
const [showLogin, setShowLogin] = useState(false);
return ( return (
<Layout> <Layout>
{auth ? ( {isAuthenticated ? (
<UserTable /> <UserTable isAuthenticated={isAuthenticated} />
) : ( ) : (
<div className="flex items-center justify-center h-full"> <>
<h2 className="text-2xl font-bold text-gray-700 dark:text-gray-300"> <div className="flex items-center justify-center h-full">
Please log in to view the user table. <h2 className="text-2xl font-bold text-gray-700 dark:text-gray-300">
</h2> Please log in to view the user table.
</div> </h2>
<button
className="ml-4 px-4 py-2 bg-blue-600 text-white rounded"
onClick={() => setShowLogin(true)}
>
Login
</button>
</div>
{showLogin && (
<LoginCard
onClose={() => setShowLogin(false)}
changeAuth={(loggedIn) => setIsAuthenticated(loggedIn)}
/>
)}
</>
)} )}
</Layout> </Layout>
); );

View File

@@ -24,11 +24,11 @@ const LoginCard: React.FC<LoginCardProps> = ({ onClose, changeAuth }) => {
onSubmit={async (event) => { onSubmit={async (event) => {
event.preventDefault(); event.preventDefault();
const formData = new FormData(event.currentTarget); const formData = new FormData(event.currentTarget);
const username = formData.get("username"); const username = formData.get("username") as string;
const password = formData.get("password"); const password = formData.get("password") as string;
loginUser(username as string, password as string); const success = await loginUser(username, password); // Make loginUser return true/false
if (changeAuth) { if (success) {
changeAuth(true); changeAuth?.(true);
onClose(); onClose();
} }
}} }}

View File

@@ -1,7 +1,6 @@
import { MoreVertical } from "lucide-react"; import { MoreVertical } from "lucide-react";
import { useContext, useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useUsers } from "../utils/useUsers.ts"; import { useUsers } from "../utils/useUsers.ts";
import { AuthContext } from "../utils/context.tsx";
interface User { interface User {
id: number; id: number;
@@ -14,7 +13,11 @@ interface User {
role: string; role: string;
} }
const UserTable = () => { interface UserTableProps {
isAuthenticated: boolean;
}
const UserTable: React.FC<UserTableProps> = ({ isAuthenticated }) => {
const [openMenu, setOpenMenu] = useState<number | null>(null); const [openMenu, setOpenMenu] = useState<number | null>(null);
const { const {
@@ -25,21 +28,16 @@ const UserTable = () => {
updateUser: updateUserFunc, updateUser: updateUserFunc,
} = useUsers(); } = useUsers();
const refresh = () => {
refreshUsers();
};
const auth = useContext(AuthContext);
useEffect(() => { useEffect(() => {
refresh(); if (isAuthenticated) {
}, [auth]); refreshUsers();
}
}, [isAuthenticated]);
const handleMenuClick = (userId: number) => { const handleMenuClick = (userId: number) => {
setOpenMenu(openMenu === userId ? null : userId); setOpenMenu(openMenu === userId ? null : userId);
}; };
const handleMenuClose = () => setOpenMenu(null); const handleMenuClose = () => setOpenMenu(null);
const handleInputChange = (id: number, field: keyof User, value: string) => { const handleInputChange = (id: number, field: keyof User, value: string) => {

View File

@@ -1,27 +1,26 @@
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { myToast } from "./frontendService"; import { myToast } from "./frontendService";
export const loginUser = (username: string, password: string) => { export const loginUser = async (
fetch("http://localhost:5002/api/login", { username: string,
method: "POST", password: string
headers: { "Content-Type": "application/json" }, ): Promise<boolean> => {
body: JSON.stringify({ username, password }), try {
}) const response = await fetch("http://localhost:5002/api/login", {
.then(async (response) => { method: "POST",
if (response.ok) { headers: { "Content-Type": "application/json" },
const data = await response.json(); body: JSON.stringify({ username, password }),
Cookies.set("token", data.token, { expires: 7 });
Cookies.set("name", data.user.first_name, { expires: 7 });
myToast("Logged in successfully!", "success");
} else if (response.status === 401) {
myToast("Invalid username or password!", "error");
} else if (response.status === 403) {
myToast("You are not an Admin!", "error");
}
})
.catch((error) => {
console.log("Login failed: ", error);
}); });
if (response.ok) {
const data = await response.json();
Cookies.set("token", data.token); // Set cookie here
Cookies.set("name", data.name);
return true;
}
return false;
} catch {
return false;
}
}; };
export const logout = () => { export const logout = () => {