Compare commits
4 Commits
debian12ne
...
main
Author | SHA1 | Date | |
---|---|---|---|
9241257d30 | |||
6a8d13b69b | |||
7abed30091 | |||
850c475329 |
110
backend/routes/api.js
Normal file
110
backend/routes/api.js
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import express from "express";
|
||||||
|
import {
|
||||||
|
loginUser,
|
||||||
|
createUser,
|
||||||
|
updateUser,
|
||||||
|
deleteUser,
|
||||||
|
getAllUsers,
|
||||||
|
} from "../services/database.js";
|
||||||
|
import { generateToken, authenticate } from "../services/tokenService.js";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.post("/login", async (req, res) => {
|
||||||
|
try {
|
||||||
|
const result = await loginUser(req.body.username, req.body.password);
|
||||||
|
if (result.success && result.user.role === "admin") {
|
||||||
|
const userToken = await generateToken({
|
||||||
|
role: result.user.role,
|
||||||
|
username: result.user.username,
|
||||||
|
});
|
||||||
|
console.log("User token generated: ", userToken);
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: "Login successful",
|
||||||
|
token: userToken,
|
||||||
|
...result,
|
||||||
|
});
|
||||||
|
} else if (result.success && result.user.role === "user") {
|
||||||
|
res.status(403).json(result, { message: "You are not an Admin!" }); // Event Handler is in LoginCard.tsx - there is defined what happens when the status is 403
|
||||||
|
} else {
|
||||||
|
res.status(401).json(result, { message: "Invalid credentials" }); // Event Handler is in LoginCard.tsx - there is defined what happens when the status is 401
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error logging in:", err);
|
||||||
|
res.status(500).json({ success: false, message: "Internal server error" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/getAllUsers", authenticate, async (req, res) => {
|
||||||
|
if (req.user.role === "admin") {
|
||||||
|
getAllUsers()
|
||||||
|
.then((users) => {
|
||||||
|
res.status(200).json(users);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error fetching users:", err);
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json({ success: false, message: "Internal server error" });
|
||||||
|
});
|
||||||
|
console.log("Fetched all users successfully");
|
||||||
|
} else if (req.user.role === "user") {
|
||||||
|
res.status(403).json({ success: false, message: "Access denied" });
|
||||||
|
console.log("Access denied for user role");
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ success: false, message: "Server error" });
|
||||||
|
console.log("Server error while fetching users");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/deleteUser", authenticate, async (req, res) => {
|
||||||
|
if (req.user.role === "admin") {
|
||||||
|
deleteUser(req.body.id)
|
||||||
|
.then((result) => {
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result);
|
||||||
|
} else {
|
||||||
|
throw new Error("Failed to delete user");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error deleting user:", err);
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json({ success: false, message: "Internal server error" });
|
||||||
|
});
|
||||||
|
console.log("User deleted successfully");
|
||||||
|
} else {
|
||||||
|
console.log("Access denied for user role");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post("/updateUser", authenticate, async (req, res) => {
|
||||||
|
if (req.user.role === "admin") {
|
||||||
|
updateUser(
|
||||||
|
req.body.username,
|
||||||
|
req.body.first_name,
|
||||||
|
req.body.last_name,
|
||||||
|
req.body.password,
|
||||||
|
req.body.email,
|
||||||
|
req.body.id
|
||||||
|
)
|
||||||
|
.then((result) => {
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result);
|
||||||
|
} else {
|
||||||
|
throw new Error("Failed to update user");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error updating user:", err);
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json({ success: false, message: "Internal server error" });
|
||||||
|
});
|
||||||
|
console.log("User updated successfully");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
@@ -3,15 +3,8 @@ import express from "express";
|
|||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 5002;
|
const port = 5002;
|
||||||
import {
|
|
||||||
loginUser,
|
|
||||||
createUser,
|
|
||||||
updateUser,
|
|
||||||
deleteUser,
|
|
||||||
getAllUsers,
|
|
||||||
} from "./services/database.js";
|
|
||||||
import { generateToken, authenticate } from "./services/tokenService.js";
|
|
||||||
import cookieParser from "cookie-parser";
|
import cookieParser from "cookie-parser";
|
||||||
|
import router from "./routes/api.js";
|
||||||
|
|
||||||
//view engine ejs
|
//view engine ejs
|
||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
@@ -20,105 +13,10 @@ app.use(express.json());
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
app.post("/api/login", async (req, res) => {
|
app.use("/api", router);
|
||||||
try {
|
|
||||||
const result = await loginUser(req.body.username, req.body.password);
|
|
||||||
if (result.success && result.user.role === "admin") {
|
|
||||||
const userToken = await generateToken({
|
|
||||||
role: result.user.role,
|
|
||||||
username: result.user.username,
|
|
||||||
});
|
|
||||||
console.log("User token generated: ", userToken);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
message: "Login successful",
|
|
||||||
token: userToken,
|
|
||||||
...result,
|
|
||||||
});
|
|
||||||
} else if (result.success && result.user.role === "user") {
|
|
||||||
res.status(403).json(result, { message: "You are not an Admin!" }); // Event Handler is in LoginCard.tsx - there is defined what happens when the status is 403
|
|
||||||
} else {
|
|
||||||
res.status(401).json(result, { message: "Invalid credentials" }); // Event Handler is in LoginCard.tsx - there is defined what happens when the status is 401
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error logging in:", err);
|
|
||||||
res.status(500).json({ success: false, message: "Internal server error" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/api/getAllUsers", authenticate, async (req, res) => {
|
|
||||||
if (req.user.role === "admin") {
|
|
||||||
getAllUsers()
|
|
||||||
.then((users) => {
|
|
||||||
res.status(200).json(users);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error("Error fetching users:", err);
|
|
||||||
res
|
|
||||||
.status(500)
|
|
||||||
.json({ success: false, message: "Internal server error" });
|
|
||||||
});
|
|
||||||
console.log("Fetched all users successfully");
|
|
||||||
} else if (req.user.role === "user") {
|
|
||||||
res.status(403).json({ success: false, message: "Access denied" });
|
|
||||||
console.log("Access denied for user role");
|
|
||||||
} else {
|
|
||||||
res.status(500).json({ success: false, message: "Server error" });
|
|
||||||
console.log("Server error while fetching users");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post("/api/deleteUser", authenticate, async (req, res) => {
|
|
||||||
if (req.user.role === "admin") {
|
|
||||||
deleteUser(req.body.id)
|
|
||||||
.then((result) => {
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json(result);
|
|
||||||
} else {
|
|
||||||
throw new Error("Failed to delete user");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error("Error deleting user:", err);
|
|
||||||
res
|
|
||||||
.status(500)
|
|
||||||
.json({ success: false, message: "Internal server error" });
|
|
||||||
});
|
|
||||||
console.log("User deleted successfully");
|
|
||||||
} else {
|
|
||||||
console.log("Access denied for user role");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post("/api/updateUser", authenticate, async (req, res) => {
|
|
||||||
if (req.user.role === "admin") {
|
|
||||||
updateUser(
|
|
||||||
req.body.username,
|
|
||||||
req.body.first_name,
|
|
||||||
req.body.last_name,
|
|
||||||
req.body.password,
|
|
||||||
req.body.email,
|
|
||||||
req.body.id
|
|
||||||
)
|
|
||||||
.then((result) => {
|
|
||||||
if (result.success) {
|
|
||||||
res.status(200).json(result);
|
|
||||||
} else {
|
|
||||||
throw new Error("Failed to update user");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error("Error updating user:", err);
|
|
||||||
res
|
|
||||||
.status(500)
|
|
||||||
.json({ success: false, message: "Internal server error" });
|
|
||||||
});
|
|
||||||
console.log("User updated successfully");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Express backend server is running at Port: ${port}`);
|
console.log(`Express backend server is running at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// error handling code
|
// error handling code
|
||||||
|
@@ -1,42 +1,33 @@
|
|||||||
services:
|
services:
|
||||||
admin_react-frontend:
|
# admin_react-frontend:
|
||||||
container_name: bikelane-frontend_react-admin
|
# container_name: bikelane-frontend_react-admin
|
||||||
build: ./frontend_admin
|
# build: ./frontend_admin
|
||||||
ports:
|
# ports:
|
||||||
- "5001:5001"
|
# - "5001:5001"
|
||||||
networks:
|
# environment:
|
||||||
- proxynet
|
# - CHOKIDAR_USEPOLLING=true
|
||||||
- bikelane_network
|
# volumes:
|
||||||
environment:
|
# - ./frontend_admin:/app
|
||||||
- CHOKIDAR_USEPOLLING=true
|
# - /app/node_modules
|
||||||
volumes:
|
# restart: unless-stopped
|
||||||
- ./frontend_admin:/app
|
|
||||||
- /app/node_modules
|
|
||||||
restart: unless-stopped
|
|
||||||
|
|
||||||
user_react-frontend:
|
# user_react-frontend:
|
||||||
container_name: bikelane-frontend_react-user
|
# container_name: bikelane-frontend_react-user
|
||||||
build: ./frontend_user
|
# build: ./frontend_user
|
||||||
networks:
|
# ports:
|
||||||
- proxynet
|
# - "5003:5003"
|
||||||
- bikelane_network
|
# environment:
|
||||||
ports:
|
# - CHOKIDAR_USEPOLLING=true
|
||||||
- "5003:5003"
|
# volumes:
|
||||||
environment:
|
# - ./frontend_user:/app
|
||||||
- CHOKIDAR_USEPOLLING=true
|
# - /app/node_modules
|
||||||
volumes:
|
# restart: unless-stopped
|
||||||
- ./frontend_user:/app
|
|
||||||
- /app/node_modules
|
|
||||||
restart: unless-stopped
|
|
||||||
|
|
||||||
bikelane-backend:
|
bikelane-backend:
|
||||||
container_name: bikelane-backend_express
|
container_name: bikelane-backend_express
|
||||||
build: ./backend
|
build: ./backend
|
||||||
ports:
|
ports:
|
||||||
- "5002:5002"
|
- "5002:5002"
|
||||||
networks:
|
|
||||||
- proxynet
|
|
||||||
- bikelane_network
|
|
||||||
environment:
|
environment:
|
||||||
DB_HOST: mysql
|
DB_HOST: mysql
|
||||||
DB_USER: root
|
DB_USER: root
|
||||||
@@ -52,8 +43,6 @@ services:
|
|||||||
container_name: bikelane-mysql
|
container_name: bikelane-mysql
|
||||||
image: mysql:8.0
|
image: mysql:8.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
|
||||||
- bikelane_network
|
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ROOT_PASSWORD: D7Ze0lwV9hMrNQHdz1Q8yi0MIQuOO8
|
MYSQL_ROOT_PASSWORD: D7Ze0lwV9hMrNQHdz1Q8yi0MIQuOO8
|
||||||
MYSQL_DATABASE: bikelane
|
MYSQL_DATABASE: bikelane
|
||||||
@@ -64,9 +53,3 @@ services:
|
|||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mysql-data:
|
mysql-data:
|
||||||
|
|
||||||
networks:
|
|
||||||
proxynet:
|
|
||||||
external: true
|
|
||||||
bikelane_network:
|
|
||||||
external: false
|
|
||||||
|
1
frontend_admin/.env
Normal file
1
frontend_admin/.env
Normal file
@@ -0,0 +1 @@
|
|||||||
|
REACT_APP_SERVER_URL=http://localhost:5002
|
@@ -1,13 +1,12 @@
|
|||||||
# Build Stage
|
FROM node:20-alpine
|
||||||
FROM node:20-alpine AS build
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
# Production Stage
|
|
||||||
FROM nginx:stable-alpine AS production
|
|
||||||
COPY --from=build /app/build /usr/share/nginx/html
|
|
||||||
EXPOSE 5001
|
EXPOSE 5001
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
|
CMD ["npm", "start"]
|
||||||
|
67
frontend_admin/other/App copy.tsx
Normal file
67
frontend_admin/other/App copy.tsx
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import React, { useState } from "react";
|
||||||
|
import "./App.css";
|
||||||
|
import Header from "./Header";
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [username, setUsername] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:5002/api/login", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
console.log("Login successful");
|
||||||
|
// Handle successful login here
|
||||||
|
} else {
|
||||||
|
console.log("Login failed");
|
||||||
|
// Handle login error here
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Login error:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="username">Username:</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="username"
|
||||||
|
name="username"
|
||||||
|
value={username}
|
||||||
|
onChange={(e) => setUsername(e.target.value)}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="password">Password:</label>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="password"
|
||||||
|
name="password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
30
frontend_admin/other/App.tsx
Normal file
30
frontend_admin/other/App.tsx
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import React, { useState } from "react";
|
||||||
|
import Header from "./components/Header";
|
||||||
|
import LoginCard from "./components/LoginCard";
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [showLogin, setShowLogin] = useState(false);
|
||||||
|
|
||||||
|
const handleLoginClick = () => setShowLogin(true);
|
||||||
|
const handleCloseLogin = () => setShowLogin(false);
|
||||||
|
|
||||||
|
const handleLoginSubmit = (username: string, password: string) => {
|
||||||
|
// Hier kannst du fetch einbauen
|
||||||
|
alert(`Login: ${username} / ${password}`);
|
||||||
|
setShowLogin(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-white">
|
||||||
|
<Header onLoginClick={handleLoginClick} />
|
||||||
|
<main className="pt-20 w-full h-full flex flex-col items-center justify-start">
|
||||||
|
{/* Hier kommt später der Seiteninhalt */}
|
||||||
|
{showLogin && (
|
||||||
|
<LoginCard onClose={handleCloseLogin} onSubmit={handleLoginSubmit} />
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
29
frontend_admin/other/components/Header.tsx
Normal file
29
frontend_admin/other/components/Header.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import React from "react";
|
||||||
|
import bike from "../../src/assets/bicycle-solid.svg";
|
||||||
|
import user from "../../src/assets/circle-user-solid.svg";
|
||||||
|
|
||||||
|
type HeaderProps = {
|
||||||
|
onLoginClick: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const Header: React.FC<HeaderProps> = ({ onLoginClick }) => (
|
||||||
|
<header className="fixed top-0 left-0 w-full h-20 bg-[#101c5e] flex items-center justify-between px-10 z-50 shadow">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<img src={bike} alt="Bike Logo" className="h-10 w-10" />
|
||||||
|
<span className="text-white text-2xl font-semibold select-none">
|
||||||
|
Bikelane <b>Web</b>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<img src={user} alt="User Icon" className="h-8 w-8" />
|
||||||
|
<button
|
||||||
|
onClick={onLoginClick}
|
||||||
|
className="bg-white text-[#101c5e] font-bold px-5 py-1 rounded-lg text-lg border-2 border-white hover:bg-gray-200 transition"
|
||||||
|
>
|
||||||
|
Login
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default Header;
|
72
frontend_admin/other/components/LoginCard.tsx
Normal file
72
frontend_admin/other/components/LoginCard.tsx
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import React, { useRef, useEffect } from "react";
|
||||||
|
|
||||||
|
type LoginCardProps = {
|
||||||
|
onClose: () => void;
|
||||||
|
onSubmit: (username: string, password: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const LoginCard: React.FC<LoginCardProps> = ({ onClose, onSubmit }) => {
|
||||||
|
const [username, setUsername] = React.useState("");
|
||||||
|
const [password, setPassword] = React.useState("");
|
||||||
|
const cardRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handler = (e: MouseEvent) => {
|
||||||
|
if (cardRef.current && !cardRef.current.contains(e.target as Node)) {
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener("mousedown", handler);
|
||||||
|
return () => document.removeEventListener("mousedown", handler);
|
||||||
|
}, [onClose]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed inset-0 bg-black bg-opacity-30 flex items-center justify-center z-40">
|
||||||
|
<div
|
||||||
|
ref={cardRef}
|
||||||
|
className="bg-gray-200 rounded-xl shadow-lg w-full max-w-md flex flex-col"
|
||||||
|
>
|
||||||
|
<div className="bg-[#101c5e] text-white rounded-t-xl px-8 py-4 text-center text-2xl font-bold">
|
||||||
|
Login
|
||||||
|
</div>
|
||||||
|
<form
|
||||||
|
className="flex flex-col gap-5 px-8 py-6"
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
onSubmit(username, password);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<label className="font-bold">
|
||||||
|
username
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
className="mt-2 w-full rounded-full px-5 py-2 bg-gray-400 outline-none"
|
||||||
|
value={username}
|
||||||
|
onChange={(e) => setUsername(e.target.value)}
|
||||||
|
autoFocus
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="font-bold">
|
||||||
|
password
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
className="mt-2 w-full rounded-full px-5 py-2 bg-gray-400 outline-none"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="bg-[#101c5e] text-white font-bold rounded-lg py-3 mt-6 text-xl hover:bg-[#203080] transition"
|
||||||
|
>
|
||||||
|
Login
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LoginCard;
|
13
frontend_admin/package-lock.json
generated
13
frontend_admin/package-lock.json
generated
@@ -12,6 +12,7 @@
|
|||||||
"@tanstack/react-table": "^8.21.3",
|
"@tanstack/react-table": "^8.21.3",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
|
"dotenv": "^17.2.0",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"lucide-react": "^0.525.0",
|
"lucide-react": "^0.525.0",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
@@ -2640,6 +2641,18 @@
|
|||||||
"tslib": "^2.0.3"
|
"tslib": "^2.0.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/dotenv": {
|
||||||
|
"version": "17.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.2.0.tgz",
|
||||||
|
"integrity": "sha512-Q4sgBT60gzd0BB0lSyYD3xM4YxrXA9y4uBDof1JNYGzOXrQdQ6yX+7XIAqoFOGQFOTK1D3Hts5OllpxMDZFONQ==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://dotenvx.com"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/electron-to-chromium": {
|
"node_modules/electron-to-chromium": {
|
||||||
"version": "1.5.187",
|
"version": "1.5.187",
|
||||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.187.tgz",
|
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.187.tgz",
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
"@tanstack/react-table": "^8.21.3",
|
"@tanstack/react-table": "^8.21.3",
|
||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
|
"dotenv": "^17.2.0",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"lucide-react": "^0.525.0",
|
"lucide-react": "^0.525.0",
|
||||||
"react": "^19.1.0",
|
"react": "^19.1.0",
|
||||||
|
@@ -4,6 +4,7 @@ import UserTable from "./components/UserTable";
|
|||||||
import LoginCard from "./components/LoginCard";
|
import LoginCard from "./components/LoginCard";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { loadTheme } from "./utils/frontendService";
|
import { loadTheme } from "./utils/frontendService";
|
||||||
|
import { myToast } from "./utils/frontendService";
|
||||||
import "react-toastify/dist/ReactToastify.css";
|
import "react-toastify/dist/ReactToastify.css";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { AuthContext } from "./utils/context";
|
import { AuthContext } from "./utils/context";
|
||||||
@@ -11,12 +12,15 @@ import { AuthContext } from "./utils/context";
|
|||||||
function App() {
|
function App() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadTheme();
|
loadTheme();
|
||||||
|
if (Cookies.get("token")) {
|
||||||
|
myToast("User list updated", "success");
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState(
|
const [isAuthenticated, setIsAuthenticated] = useState(
|
||||||
!!Cookies.get("token")
|
!!Cookies.get("token")
|
||||||
);
|
);
|
||||||
const [, setShowLogin] = useState(false);
|
const [showLogin, setShowLogin] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
|
<AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
|
||||||
|
@@ -18,7 +18,7 @@ const LoginCard: React.FC<LoginCardProps> = ({ onClose, changeAuth }) => {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setError("");
|
setError("");
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://45.133.75.67:5002/api/login", {
|
const response = await fetch("http://localhost:5002/api/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
|
@@ -33,7 +33,7 @@ export function useUsers(): UserReturn {
|
|||||||
|
|
||||||
const fetchUsers = async () => {
|
const fetchUsers = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://45.133.75.67:5002/api/getAllUsers", {
|
const response = await fetch("http://localhost:5002/api/getAllUsers", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: headers,
|
headers: headers,
|
||||||
});
|
});
|
||||||
@@ -50,7 +50,7 @@ export function useUsers(): UserReturn {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const deleteUser = (id: number) => {
|
const deleteUser = (id: number) => {
|
||||||
fetch("http://45.133.75.67:5002/api/deleteUser", {
|
fetch("http://localhost:5002/api/deleteUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ id: id }),
|
body: JSON.stringify({ id: id }),
|
||||||
headers: {
|
headers: {
|
||||||
@@ -109,7 +109,7 @@ export function useUsers(): UserReturn {
|
|||||||
console.log("Sending user data:", userData);
|
console.log("Sending user data:", userData);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://45.133.75.67:5002/api/updateUser", {
|
const response = await fetch("http://localhost:5002/api/updateUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(userData),
|
body: JSON.stringify(userData),
|
||||||
headers: {
|
headers: {
|
||||||
|
@@ -6,7 +6,7 @@ export const loginUser = async (
|
|||||||
password: string
|
password: string
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://45.133.75.67:5002/api/login", {
|
const response = await fetch("http://localhost:5002/api/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
@@ -27,11 +27,11 @@ export const logout = () => {
|
|||||||
Cookies.remove("username");
|
Cookies.remove("username");
|
||||||
Cookies.remove("token");
|
Cookies.remove("token");
|
||||||
localStorage.removeItem("users");
|
localStorage.removeItem("users");
|
||||||
myToast("Logged out successfully!", "info");
|
myToast("Logged out successfully!", "success");
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteUser = (id: number) => {
|
export const deleteUser = (id: number) => {
|
||||||
fetch("http://45.133.75.67:5002/api/deleteUser", {
|
fetch("http://localhost:5002/api/deleteUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ id: id }),
|
body: JSON.stringify({ id: id }),
|
||||||
headers: {
|
headers: {
|
||||||
@@ -41,9 +41,9 @@ export const deleteUser = (id: number) => {
|
|||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
replaceUsers();
|
replaceUsers("User deleted successfully!");
|
||||||
} else {
|
} else {
|
||||||
alert("Failed to delete user");
|
myToast("Failed to delete user", "error");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -51,9 +51,9 @@ export const deleteUser = (id: number) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const replaceUsers = async () => {
|
export const replaceUsers = async (alertMessage: string) => {
|
||||||
localStorage.removeItem("users");
|
localStorage.removeItem("users");
|
||||||
await fetch("http://45.133.75.67:5002/api/getAllUsers", {
|
await fetch("http://localhost:5002/api/getAllUsers", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
@@ -62,7 +62,7 @@ export const replaceUsers = async () => {
|
|||||||
.then((res) => res.json())
|
.then((res) => res.json())
|
||||||
.then((users) => {
|
.then((users) => {
|
||||||
localStorage.setItem("users", JSON.stringify(users));
|
localStorage.setItem("users", JSON.stringify(users));
|
||||||
window.location.reload();
|
myToast(alertMessage, "success");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ export const updateUserFunc = async (userID: number) => {
|
|||||||
|
|
||||||
if (!usernameEl || !firstNameEl || !lastNameEl || !emailEl) {
|
if (!usernameEl || !firstNameEl || !lastNameEl || !emailEl) {
|
||||||
console.error("Required form elements not found");
|
console.error("Required form elements not found");
|
||||||
alert("Form elements not found");
|
myToast("Form elements not found", "error");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ export const updateUserFunc = async (userID: number) => {
|
|||||||
console.log("Sending user data:", userData);
|
console.log("Sending user data:", userData);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://45.133.75.67:5002/api/updateUser", {
|
const response = await fetch("http://localhost:5002/api/updateUser", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(userData),
|
body: JSON.stringify(userData),
|
||||||
headers: {
|
headers: {
|
||||||
@@ -118,7 +118,7 @@ export const updateUserFunc = async (userID: number) => {
|
|||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
console.log("User updated successfully");
|
console.log("User updated successfully");
|
||||||
replaceUsers();
|
replaceUsers("User updated successfully!");
|
||||||
} else {
|
} else {
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
console.error("Server error:", response.status, errorText);
|
console.error("Server error:", response.status, errorText);
|
||||||
|
@@ -1,13 +1,12 @@
|
|||||||
# Build Stage
|
FROM node:20-alpine
|
||||||
FROM node:20-alpine AS build
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
# Production Stage
|
|
||||||
FROM nginx:stable-alpine AS production
|
|
||||||
COPY --from=build /app/build /usr/share/nginx/html
|
|
||||||
EXPOSE 5003
|
EXPOSE 5003
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
|
CMD ["npm", "start"]
|
||||||
|
Reference in New Issue
Block a user