Skip to content

Commit

Permalink
Merge pull request #23 from moevm/manucharova_component
Browse files Browse the repository at this point in the history
fix authentification bug
  • Loading branch information
necitboss authored Dec 10, 2024
2 parents f6d129e + 88470f7 commit a3dec9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
26 changes: 18 additions & 8 deletions main/controllers/AdminController.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@ export const register = async (req, res) => {
}
);
const {passwordHash, ...AdminData} = admin._doc;

res.json({
...AdminData,
token
Expand All @@ -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({
Expand Down Expand Up @@ -80,4 +90,4 @@ export const login = async (req, res) => {
message: "Не удалось авторизоваться"
})
}
}
}
21 changes: 21 additions & 0 deletions main/controllers/ComponentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "Не удалось удалить компонент"
});
}
}
3 changes: 2 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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();

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3dec9d

Please sign in to comment.