70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
|
const changeRouter = require('express').Router();
|
||
|
|
||
|
module.exports = changeRouter;
|
||
|
|
||
|
const { users, getUserFromDB, deleteUserFromDB, addUserToDB } = 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)'});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const updatedUser = {
|
||
|
"nickname": newNickname,
|
||
|
"password": user.password,
|
||
|
"id": user.id
|
||
|
};
|
||
|
|
||
|
// Delete the old one
|
||
|
deleteUserFromDB(id)
|
||
|
|
||
|
// Insert updated
|
||
|
addUserToDB(updatedUser);
|
||
|
|
||
|
res.status(200).send({});
|
||
|
});
|
||
|
|
||
|
changeRouter.post('/password', (req, res) => {
|
||
|
const { id, newPassword } = req.body;
|
||
|
console.log("Request password in /change:", id);
|
||
|
|
||
|
const user = getUserFromDB(id);
|
||
|
|
||
|
// Invalid identification
|
||
|
if (!user) {
|
||
|
res.status(401).send({message: 'Invalid credentials (id)'});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Delete the old one
|
||
|
deleteUserFromDB(id)
|
||
|
|
||
|
// Insert updated
|
||
|
const updatedUser = {
|
||
|
"nickname": user.nickname,
|
||
|
"password": newPassword,
|
||
|
"id": user.id
|
||
|
};
|
||
|
addUserToDB(updatedUser);
|
||
|
|
||
|
res.status(200).send({});
|
||
|
});
|
||
|
|
||
|
changeRouter.delete('/:id', (req, res) => {
|
||
|
const { id } = req.params;
|
||
|
console.log("Request delete in /change:", id);
|
||
|
|
||
|
deleteUserFromDB(id);
|
||
|
});
|
||
|
|