addded error codes and improved error handling

This commit is contained in:
2026-06-04 15:32:51 +02:00
parent faebe54db3
commit c42bdea047
19 changed files with 730 additions and 96 deletions
@@ -72,3 +72,20 @@ export const getSettings = async () => {
return { code: "eu005" };
}
};
export const changePassword = async (
username,
currentPasswordUser,
newPassword,
) => {
const [result] = await pool.query(
`UPDATE users SET password = ? WHERE username = ? AND password = ?;`,
[newPassword, username, currentPasswordUser],
);
if (result.affectedRows > 0) {
return { code: "su005" };
} else {
return { code: "eu006" };
}
};
+24 -3
View File
@@ -6,6 +6,7 @@ import {
loginUser,
updateSettings,
getSettings,
changePassword,
} from "./database/users.database.js";
dotenv.config();
const router = express.Router();
@@ -18,8 +19,6 @@ router.post("/update-app-settings", authenticate, async (req, res) => {
const appName = req.body.appName;
const currency = req.body.currency;
console.log(req.body);
const result = await updateSettings(req.body);
if (result.code === "su003") {
@@ -91,7 +90,7 @@ router.post("/login", async (req, res) => {
const token = await generateToken(result.data);
const login = await loginUser(result.data.username);
if (login.code === "e003") {
if (login.code === "eu003") {
res.status(500).json({
success: false,
code: "eu003",
@@ -111,4 +110,26 @@ router.post("/login", async (req, res) => {
}
});
router.post("/change-password", authenticate, async (req, res) => {
const currentPassword = req.body.currentPassword;
const newPassword = req.body.newPassword;
const username = req.user.username;
const result = await changePassword(username, currentPassword, newPassword);
if (result.code === "su005") {
res.status(202).json({
success: true,
code: result.code,
});
}
if (result.code === "eu006") {
res.status(406).json({
success: false,
code: result.code,
});
}
});
export default router;