refactor user editing functionality to remove password handling
This commit is contained in:
@@ -237,7 +237,6 @@ const UserTable: React.FC = () => {
|
|||||||
user.id,
|
user.id,
|
||||||
user.username,
|
user.username,
|
||||||
user.role,
|
user.role,
|
||||||
user.password
|
|
||||||
).then((response) => {
|
).then((response) => {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
setError(
|
setError(
|
||||||
|
@@ -24,19 +24,18 @@ export const handleDelete = async (userId: number) => {
|
|||||||
export const handleEdit = async (
|
export const handleEdit = async (
|
||||||
userId: number,
|
userId: number,
|
||||||
username: string,
|
username: string,
|
||||||
role: string,
|
role: string
|
||||||
password: string
|
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`http://localhost:8002/api/editUser/${userId}`,
|
`http://localhost:8002/api/editUser/${userId}`,
|
||||||
{
|
{
|
||||||
method: "PUT",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Authorization: `Bearer ${Cookies.get("token")}`,
|
Authorization: `Bearer ${Cookies.get("token")}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ username, role, password }),
|
body: JSON.stringify({ username, role }),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
@@ -224,10 +224,10 @@ router.get("/verifyToken", authenticate, async (req, res) => {
|
|||||||
res.status(200).json({ message: "Token is valid" });
|
res.status(200).json({ message: "Token is valid" });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put("/editUser/:id", authenticate, async (req, res) => {
|
router.post("/editUser/:id", authenticate, async (req, res) => {
|
||||||
const userId = req.params.id;
|
const userId = req.params.id;
|
||||||
const { username, role, password } = req.body || {};
|
const { username, role } = req.body || {};
|
||||||
const result = await handleEdit(userId, username, role, password);
|
const result = await handleEdit(userId, username, role);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
return res.status(200).json({ message: "User edited successfully" });
|
return res.status(200).json({ message: "User edited successfully" });
|
||||||
}
|
}
|
||||||
|
@@ -340,10 +340,10 @@ export const deleteUserID = async (userId) => {
|
|||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const handleEdit = async (userId, username, role, password) => {
|
export const handleEdit = async (userId, username, role) => {
|
||||||
const [result] = await pool.query(
|
const [result] = await pool.query(
|
||||||
"UPDATE users SET username = ?, role = ?, password = ? WHERE id = ?",
|
"UPDATE users SET username = ?, role = ? WHERE id = ?",
|
||||||
[username, role, password, userId]
|
[username, role, userId]
|
||||||
);
|
);
|
||||||
if (result.affectedRows > 0) return { success: true };
|
if (result.affectedRows > 0) return { success: true };
|
||||||
return { success: false };
|
return { success: false };
|
||||||
|
Reference in New Issue
Block a user