all bugs removed
This commit is contained in:
@@ -13,14 +13,11 @@ function App() {
|
||||
loadTheme();
|
||||
}, []);
|
||||
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(!!Cookies.get("token"));
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(
|
||||
!!Cookies.get("token")
|
||||
);
|
||||
const [showLogin, setShowLogin] = useState(false);
|
||||
|
||||
const handleLogout = () => {
|
||||
Cookies.remove("token");
|
||||
setIsAuthenticated(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
|
||||
<Layout>
|
||||
@@ -44,6 +41,4 @@ function App() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default App;
|
||||
|
@@ -1,15 +1,17 @@
|
||||
import React, { useContext } from "react";
|
||||
import Cookies from "js-cookie";
|
||||
import { AuthContext } from "../utils/context";
|
||||
import { myToast } from "../utils/frontendService";
|
||||
|
||||
const Header: React.FC = () => {
|
||||
const { isAuthenticated, setIsAuthenticated } = useContext(AuthContext);
|
||||
const username = Cookies.get("username");
|
||||
const firstName = Cookies.get("firstName");
|
||||
|
||||
const handleLogout = () => {
|
||||
Cookies.remove("token");
|
||||
Cookies.remove("username");
|
||||
Cookies.remove("firstName");
|
||||
setIsAuthenticated(false);
|
||||
myToast("Logged out successfully!", "info");
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -21,7 +23,7 @@ const Header: React.FC = () => {
|
||||
{isAuthenticated && (
|
||||
<>
|
||||
<span className="text-lg font-semibold text-blue-700 dark:text-blue-200">
|
||||
Hello, {username || "User"}
|
||||
Hello, {firstName || "User"}
|
||||
</span>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import React, { useState, useContext } from "react";
|
||||
import Cookies from "js-cookie";
|
||||
import { AuthContext } from "../utils/context";
|
||||
import { myToast } from "../utils/frontendService";
|
||||
|
||||
interface LoginCardProps {
|
||||
onClose: () => void;
|
||||
@@ -25,11 +26,13 @@ const LoginCard: React.FC<LoginCardProps> = ({ onClose, changeAuth }) => {
|
||||
const data = await response.json();
|
||||
if (response.ok && data.token) {
|
||||
Cookies.set("token", data.token);
|
||||
Cookies.set("username", username);
|
||||
Cookies.set("firstName", data.user.first_name);
|
||||
setIsAuthenticated(true);
|
||||
if (changeAuth) changeAuth(true);
|
||||
onClose();
|
||||
myToast("Login successful!", "success");
|
||||
} else {
|
||||
myToast("Login failed!", "error");
|
||||
setError(data.message || "Login fehlgeschlagen");
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -82,4 +85,3 @@ const LoginCard: React.FC<LoginCardProps> = ({ onClose, changeAuth }) => {
|
||||
};
|
||||
|
||||
export default LoginCard;
|
||||
|
||||
|
@@ -5,5 +5,5 @@ export const AuthContext = React.createContext<{
|
||||
setIsAuthenticated: (auth: boolean) => void;
|
||||
}>({
|
||||
isAuthenticated: false,
|
||||
setIsAuthenticated: () => { },
|
||||
setIsAuthenticated: () => {},
|
||||
});
|
||||
|
@@ -1,3 +1,3 @@
|
||||
import { createContext } from 'react';
|
||||
import { createContext } from "react";
|
||||
|
||||
export const AuthContext = createContext(false);
|
||||
|
@@ -47,7 +47,7 @@ export function useUsers(): UserReturn {
|
||||
} catch (error) {
|
||||
console.error("Error fetching users:", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const deleteUser = (id: number) => {
|
||||
fetch("http://localhost:5002/api/deleteUser", {
|
||||
@@ -141,13 +141,11 @@ export function useUsers(): UserReturn {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
return {
|
||||
users: users,
|
||||
setUsers: setUsers,
|
||||
refresh: () => fetchUsers(),
|
||||
deleteUser: (id: number) => deleteUser(id),
|
||||
updateUser: (id: number) => updateUserFunc(id),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user