front/stubs/api/change/index.js

47 lines
1017 B
JavaScript
Raw Normal View History

2024-10-03 22:35:39 +03:00
const changeRouter = require('express').Router();
module.exports = changeRouter;
const { users, getUserFromDB } = require('../db');
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)'});
}
// Delete the old one
const index = users.findIndex(item => item.id === id);
if (index !== -1) {
users.splice(index, 1); // Remove the old user
}
// Insert updated
users.push({
"nickname": newNickname,
"password": user.password,
"id": user.id
});
res.status(200).send({});
});
changeRouter.post('/password', (req, res) => {
const { id, newPassword } = req.body;
// ...
});
changeRouter.delete('/:id', (req, res) => {
const { id } = req.params;
// ...
});