diff --git a/admin/src/components/ChangePWform.tsx b/admin/src/components/ChangePWform.tsx new file mode 100644 index 0000000..161d671 --- /dev/null +++ b/admin/src/components/ChangePWform.tsx @@ -0,0 +1,95 @@ +import React from "react"; +import { Button, Card, Field, Input, Stack } from "@chakra-ui/react"; +import { changePW } from "@/utils/userActions"; + +type ChangePWformProps = { + onClose: () => void; + alert: ( + status: "success" | "error", + message: string, + description: string + ) => void; + username: string; +}; + +const ChangePWform: React.FC = ({ + onClose, + alert, + username, +}) => { + return ( +
+ + + Passwort ändern + + Füllen Sie das folgende Formular aus, um das Passwort zu ändern. + + + + + + Neues Passwort + + + + Neues Passwort widerholen + + + + + + + + + +
+ ); +}; + +export default ChangePWform; diff --git a/admin/src/components/UserTable.tsx b/admin/src/components/UserTable.tsx index 6d91078..ccd47e6 100644 --- a/admin/src/components/UserTable.tsx +++ b/admin/src/components/UserTable.tsx @@ -18,6 +18,7 @@ import { handleDelete, handleEdit } from "@/utils/userActions"; import MyAlert from "./myChakra/MyAlert"; import AddForm from "./AddForm"; import { formatDateTime } from "@/utils/userFuncs"; +import ChangePWform from "./ChangePWform"; type User = { id: number; @@ -36,6 +37,8 @@ const UserTable: React.FC = () => { const [errorDsc, setErrorDsc] = useState(""); const [reload, setReload] = useState(false); const [addForm, setAddForm] = useState(false); + const [changePWform, setChangePWform] = useState(false); + const [changeUsr, setChangeUsr] = useState(""); const setError = ( status: "error" | "success", @@ -57,6 +60,11 @@ const UserTable: React.FC = () => { ); }; + const handlePasswordChange = (username: string) => { + setChangeUsr(username); + setChangePWform(true); + }; + useEffect(() => { const fetchUsers = async () => { setIsLoading(true); @@ -139,6 +147,16 @@ const UserTable: React.FC = () => { Benutzer + {changePWform && ( + { + setChangePWform(false); + setReload(!reload); + }} + alert={setError} + username={changeUsr} + /> + )} {isError && ( { Benutzername - Passwort + Passwort ändern Rolle @@ -198,12 +216,9 @@ const UserTable: React.FC = () => { /> - - handleInputChange(user.id, "password", e.target.value) - } - value={user.password} - /> + { + try { + const response = await fetch(`http://localhost:8002/api/changePWadmin`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${Cookies.get("token")}`, + }, + body: JSON.stringify({ newPassword, username }), + }); + if (!response.ok) { + throw new Error("Failed to change password"); + } + return { success: true }; + } catch (error) { + console.error("Error changing password:", error); + return { success: false }; + } +}; + export const deleteLoan = async (loanId: number) => { try { const response = await fetch( diff --git a/backend/routes/api.js b/backend/routes/api.js index a4b12c1..6ed2dc6 100644 --- a/backend/routes/api.js +++ b/backend/routes/api.js @@ -18,6 +18,7 @@ import { getAllItems, deleteItemID, createItem, + changeUserPassword, } from "../services/database.js"; import { authenticate, generateToken } from "../services/tokenService.js"; const router = express.Router(); @@ -276,4 +277,17 @@ router.post("/createItem", authenticate, async (req, res) => { return res.status(500).json({ message: "Failed to create item" }); }); +router.post("/changePWadmin", authenticate, async (req, res) => { + const newPassword = req.body.newPassword; + if (!newPassword) { + return res.status(400).json({ message: "New password is required" }); + } + + const result = await changeUserPassword(req.body.username, newPassword); + if (result.success) { + return res.status(200).json({ message: "Password changed successfully" }); + } + return res.status(500).json({ message: "Failed to change password" }); +}); + export default router; diff --git a/backend/services/database.js b/backend/services/database.js index 8848eb9..d692234 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -327,7 +327,9 @@ export const loginAdmin = async (username, password) => { }; export const getAllUsers = async () => { - const [result] = await pool.query("SELECT * FROM users"); + const [result] = await pool.query( + "SELECT id, username, role, entry_created_at FROM users" + ); if (result.length > 0) return { success: true, data: result }; return { success: false }; }; @@ -382,3 +384,12 @@ export const createItem = async (item_name, can_borrow_role) => { if (result.affectedRows > 0) return { success: true }; return { success: false }; }; + +export const changeUserPassword = async (username, newPassword) => { + const [result] = await pool.query( + "UPDATE users SET password = ? WHERE username = ?", + [newPassword, username] + ); + if (result.affectedRows > 0) return { success: true }; + return { success: false }; +};