change name action for api
This commit is contained in:
46
stubs/api/change/index.js
Normal file
46
stubs/api/change/index.js
Normal file
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
// ...
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user