MCPcopy Create free account
hub / github.com/Bitbox-Connect/Bitbox / Admin

Function Admin

client/src/component/Admin.jsx:16–186  ·  view source on GitHub ↗
({ mode })

Source from the content-addressed store, hash-verified

14import { useAuth } from '../contexts/authContext';
15
16const Admin = ({ mode }) => {
17 const [credentials, setCredentials] = useState({ email: "", password: "" });
18 const [loading, setLoading] = useState(false);
19 const navigate = useNavigate();
20 const { userLoggedIn } = useAuth();
21
22 // Conditional navigation if user is already logged in
23 useEffect(() => {
24 if (userLoggedIn) {
25 navigate('/');
26 }
27 }, [userLoggedIn, navigate]);
28
29 // Handle form submission for login
30 const handleSubmit = async (e) => {
31 e.preventDefault();
32
33 // Check for empty fields
34 if (!credentials.email || !credentials.password) {
35 toast.error("Please enter both email and password.");
36 return;
37 }
38
39 setLoading(true);
40 try {
41 const response = await fetch(`${SERVER_URI}/api/admin/login`, {
42 method: "POST",
43 headers: {
44 "Content-Type": "application/json",
45 },
46 body: JSON.stringify(credentials),
47 });
48 if (!response.ok) {
49 // Check for network or server errors
50 if (response.status === 500) {
51 toast.error("Server error. Please try again later.");
52 } else {
53 toast.error("Login failed! Please check your credentials.");
54 }
55 throw new Error("Response not ok");
56 }
57
58 const json = await response.json();
59
60 console.log(json)
61 if (json.success) {
62 // localStorage.setItem("token", json.authtoken); // this breakes
63 // setloggedin(!isloggedin);
64
65 navigate("/Dashboard");
66
67 } else {
68 toast.error(json.message || "Login failed! Invalid credentials.");
69 }
70 } catch (error) {
71 if (error.message === "NetworkError") {
72 toast.error("Network error. Please check your connection.");
73 } else {

Callers

nothing calls this directly

Calls 1

useAuthFunction · 0.90

Tested by

no test coverage detected