change name action for api
This commit is contained in:
parent
6bea0428f4
commit
8d0fadc906
@ -5,6 +5,7 @@
|
||||
"@ijl/cli": "^5.1.0",
|
||||
"@types/react": "^18.3.5",
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"emoji-mart": "^5.6.0",
|
||||
"express": "^4.19.2",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
|
@ -10,10 +10,13 @@ export const BASE_API_URL = DEV + getConfigValue("enterfront.api");
|
||||
// fetch(`${BASE_API_URL}/books/list`)
|
||||
|
||||
export async function post(path, body) {
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
const res = await fetch(`${BASE_API_URL}${path}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": token ? `Bearer ${token}` : undefined
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
@ -32,8 +35,13 @@ export async function post(path, body) {
|
||||
}
|
||||
|
||||
export async function get(path){
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
const res = await fetch(`${BASE_API_URL}${path}`, {
|
||||
method: "GET"
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Authorization": token ? `Bearer ${token}` : undefined
|
||||
}
|
||||
});
|
||||
|
||||
if (res.status === 200) {
|
||||
|
@ -1,10 +1,11 @@
|
||||
import React, {useEffect, useState} from "react";
|
||||
import AccountButtons from "../components/account/AccountButtons.jsx";
|
||||
import userIcon from "../../images/user.svg";
|
||||
import {get} from "../backend/api";
|
||||
import {get, post} from "../backend/api";
|
||||
import {displayMessage} from "../backend/notifications/notifications";
|
||||
import {MessageType} from "../backend/notifications/message";
|
||||
import HelloItem from "../components/account/HelloItem.jsx";
|
||||
import { URLs } from "../__data__/urls";
|
||||
|
||||
const Account = () => {
|
||||
const exitHandler = () => {
|
||||
@ -14,12 +15,27 @@ const Account = () => {
|
||||
localStorage.setItem("message", "Exited successfully!");
|
||||
window.location.href = "/";
|
||||
}
|
||||
const changeNameHandler = () => {}
|
||||
const changePassHandler = () => {}
|
||||
|
||||
const [nickname, setNickname] = useState("");
|
||||
const [id, setId] = useState("");
|
||||
|
||||
async function changeNameHandler () {
|
||||
// ...
|
||||
|
||||
const {ok, data} = await post('/change/nickname', {id: id, newNickname: "New Name"});
|
||||
|
||||
if (!ok) {
|
||||
displayMessage(data.message, MessageType.ERROR);
|
||||
} else {
|
||||
localStorage.setItem("message", "Name was changed");
|
||||
window.location.href = URLs.account.url;
|
||||
}
|
||||
}
|
||||
|
||||
async function changePassHandler (){
|
||||
// ...
|
||||
}
|
||||
|
||||
async function getUser() {
|
||||
const username = localStorage.getItem("username");
|
||||
if (!username) {
|
||||
|
@ -1,30 +1,16 @@
|
||||
const authRouter = require('express').Router();
|
||||
|
||||
// For cryptography
|
||||
// const bcrypt = require('bcrypt');
|
||||
|
||||
// For creating tokens
|
||||
const jwt = require('jsonwebtoken');
|
||||
const TOKEN_KEY = "5frv12e4few3r"
|
||||
|
||||
require('dotenv').config();
|
||||
const TOKEN_KEY = process.env.TOKEN_KEY;
|
||||
|
||||
|
||||
module.exports = authRouter;
|
||||
|
||||
// Read already defined users (pseudo-DB)
|
||||
const users = require('./users.json');
|
||||
const { users, getUserFromDB } = require('../db');
|
||||
|
||||
const getUserFromDB = (userID) => {
|
||||
if (!userID) {return false;}
|
||||
|
||||
// Accessing 'DB'
|
||||
const user = users.find((user) => user.id === userID);
|
||||
|
||||
if (user) {
|
||||
return user;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get a user by its id
|
||||
authRouter.get('/:id', (req, res) => {
|
||||
|
@ -1,6 +0,0 @@
|
||||
{
|
||||
"content": {
|
||||
|
||||
},
|
||||
"totalElement": 0
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"id": "1",
|
||||
"name": "Book name",
|
||||
"description": "Interesting book description"
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
const booksRouter = require('express').Router();
|
||||
|
||||
module.exports = booksRouter;
|
||||
|
||||
const books = []
|
||||
|
||||
booksRouter.get('/list', (req, res) => {
|
||||
res.send(require('./book-list.json'))
|
||||
})
|
||||
|
||||
booksRouter.post('/', (req, res) => {
|
||||
// body() can be used because of dev server
|
||||
console.log(req.body)
|
||||
books.push({
|
||||
name: req.body.name,
|
||||
})
|
||||
|
||||
res.send({
|
||||
status: 200
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
booksRouter.get('/:id', (req, res) => {
|
||||
console.log(req.params);
|
||||
|
||||
res.send(require('./book.json'));
|
||||
|
||||
// res.status(404).send()
|
||||
})
|
||||
|
||||
booksRouter.delete('/:id', (req, res) => {
|
||||
res.status(201).send({
|
||||
status: 'ok'
|
||||
})
|
||||
})
|
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;
|
||||
// ...
|
||||
});
|
||||
|
17
stubs/api/db.js
Normal file
17
stubs/api/db.js
Normal file
@ -0,0 +1,17 @@
|
||||
// Read already defined users (pseudo-DB)
|
||||
const users = require('./auth/users.json');
|
||||
|
||||
const getUserFromDB = (userID) => {
|
||||
if (!userID) {return false;}
|
||||
|
||||
// Accessing 'DB'
|
||||
const user = users.find((user) => user.id === userID);
|
||||
|
||||
if (user) {
|
||||
return user;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {users, getUserFromDB}
|
@ -1,9 +1,10 @@
|
||||
const booksRouter = require("./books");
|
||||
const changeRouter = require("./change");
|
||||
const authRouter = require("./auth");
|
||||
|
||||
const router = require('express').Router();
|
||||
|
||||
const delay = require('./middlewares/delay');
|
||||
const verify = require('./middlewares/verify');
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@ -11,3 +12,4 @@ module.exports = router;
|
||||
// router.use('/books', delay, booksRouter);
|
||||
|
||||
router.use('/auth', authRouter);
|
||||
router.use('/change', verify, changeRouter);
|
||||
|
23
stubs/api/middlewares/verify.js
Normal file
23
stubs/api/middlewares/verify.js
Normal file
@ -0,0 +1,23 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
require('dotenv').config();
|
||||
const TOKEN_KEY = process.env.TOKEN_KEY;
|
||||
|
||||
function verifyToken(req, res, next) {
|
||||
const token = req.headers['authorization']?.split(' ')[1];
|
||||
|
||||
if (!token) {
|
||||
return res.status(403).send({ message: 'No token provided' });
|
||||
}
|
||||
|
||||
// Verify token
|
||||
jwt.verify(token, TOKEN_KEY, (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).send({ message: 'Unauthorized' });
|
||||
}
|
||||
|
||||
next(); // Proceed to the next middleware or route
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = verifyToken;
|
Loading…
Reference in New Issue
Block a user