front/stubs/api/change/index.js

70 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-10-03 22:35:39 +03:00
const changeRouter = require('express').Router();
module.exports = changeRouter;
2024-10-04 11:21:21 +03:00
const { users, getUserFromDB, deleteUserFromDB, addUserToDB } = require('../db');
2024-10-03 22:35:39 +03:00
const jwt = require("jsonwebtoken");
changeRouter.post('/nickname', (req, res) => {
const { id, newNickname } = req.body;
console.log("Request nickname in /change:", id);
const user = getUserFromDB(id);
// Invalid identification
if (!user) {
res.status(401).send({message: 'Invalid credentials (id)'});
return;
2024-10-03 22:35:39 +03:00
}
2024-10-04 11:21:21 +03:00
const updatedUser = {
2024-10-03 22:35:39 +03:00
"nickname": newNickname,
"password": user.password,
"id": user.id
2024-10-04 11:21:21 +03:00
};
// Delete the old one
deleteUserFromDB(id)
// Insert updated
addUserToDB(updatedUser);
2024-10-03 22:35:39 +03:00
res.status(200).send({});
});
changeRouter.post('/password', (req, res) => {
const { id, newPassword } = req.body;
2024-10-04 11:21:21 +03:00
console.log("Request password in /change:", id);
const user = getUserFromDB(id);
// Invalid identification
if (!user) {
res.status(401).send({message: 'Invalid credentials (id)'});
return;
2024-10-04 11:21:21 +03:00
}
// Delete the old one
deleteUserFromDB(id)
// Insert updated
const updatedUser = {
"nickname": user.nickname,
"password": newPassword,
"id": user.id
};
addUserToDB(updatedUser);
res.status(200).send({});
2024-10-03 22:35:39 +03:00
});
changeRouter.delete('/:id', (req, res) => {
const { id } = req.params;
2024-10-04 11:21:21 +03:00
console.log("Request delete in /change:", id);
deleteUserFromDB(id);
2024-10-03 22:35:39 +03:00
});