From df64eb8ea55fec7f3f69fe80d7ca34445144999d Mon Sep 17 00:00:00 2001 From: ifdjhxh Date: Sun, 8 Dec 2024 23:12:29 +0300 Subject: [PATCH 1/2] fix authentification bug --- main/controllers/AdminController.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/main/controllers/AdminController.js b/main/controllers/AdminController.js index 2d315d3..2518ed1 100644 --- a/main/controllers/AdminController.js +++ b/main/controllers/AdminController.js @@ -1,14 +1,14 @@ import AdminModel from "../models/Admin.js"; - + import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; - + export const register = async (req, res) => { try { const password = req.body.password; const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(password, salt); - + const doc = new AdminModel({ login: req.body.login, passwordHash: hash @@ -23,7 +23,7 @@ export const register = async (req, res) => { } ); const {passwordHash, ...AdminData} = admin._doc; - + res.json({ ...AdminData, token @@ -34,21 +34,31 @@ export const register = async (req, res) => { }) } } - + export const login = async (req, res) => { try { const msg = "Неверный логин или пароль"; - if (!AdminModel.find()[0]) { + let hasAdmin = true; + + await AdminModel.find() + .then((docs) => { + if (docs.length === 0) { + hasAdmin = false; + } + console.log("first") + }) + if (!hasAdmin) { const password = "12345"; const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(password, salt); - + const doc = new AdminModel({ login: "admin", passwordHash: hash }) await doc.save(); } + console.log("second") const admin = await AdminModel.findOne({login: req.body.login}); if (!admin) { return res.status(404).json({ @@ -80,4 +90,4 @@ export const login = async (req, res) => { message: "Не удалось авторизоваться" }) } -} +} \ No newline at end of file From 88470f76094fc91a5ff37aa062bb3e88d06cb28b Mon Sep 17 00:00:00 2001 From: ifdjhxh Date: Sun, 8 Dec 2024 23:14:50 +0300 Subject: [PATCH 2/2] Server: delete component --- main/controllers/ComponentsController.js | 21 +++++++++++++++++++++ main/index.js | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/main/controllers/ComponentsController.js b/main/controllers/ComponentsController.js index 87ca49c..420ab51 100644 --- a/main/controllers/ComponentsController.js +++ b/main/controllers/ComponentsController.js @@ -119,4 +119,25 @@ export const update = async (req, res) => { message: "Не удалось обновить компонент" }) } +} + +export const deleteElem = async (req, res) => { + try { + const componentId = String(req.params.id); + await ComponentsModel.deleteOne({_id: componentId}) + .then(() => { + res.json({success: true}); + }) + .catch((e) => { + console.warn(e); + res.json({ + message: "Не удалось удалить компонент" + }); + }); + }catch (e) { + console.warn(e); + res.json({ + message: "Не удалось удалить компонент" + }); + } } \ No newline at end of file diff --git a/main/index.js b/main/index.js index d3d51de..12ffa45 100644 --- a/main/index.js +++ b/main/index.js @@ -3,11 +3,11 @@ import mongoose from "mongoose"; import cors from "cors"; import {login} from "./controllers/AdminController.js"; import checkAuth from "./utils/checkAuth.js"; -import {addComponent, getAll, getOne, update} from "./controllers/ComponentsController.js"; +import {addComponent, deleteElem, getAll, getOne, update} from "./controllers/ComponentsController.js"; import path from 'path'; const app = express(); -const isProduction = false; +const isProduction = true; const folder = isProduction? "dist": "_public"; const PORT = 4444; @@ -52,6 +52,7 @@ app.post('/components', checkAuth, addComponent); app.get('/components', getAll); app.get('/components/:id', getOne); app.patch('/components/:id', checkAuth, update); +app.delete('/components/:id', checkAuth, deleteElem); app.get('/auth/authorized', checkAuth, (req, res) => { res.json({ message: true