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