Skip to content

Commit

Permalink
Merge pull request #12 from moevm/bogdanov_admin
Browse files Browse the repository at this point in the history
Server: Admin
  • Loading branch information
necitboss authored Nov 10, 2024
2 parents 4a0196f + 0b9ea2d commit dd49aad
Show file tree
Hide file tree
Showing 6 changed files with 841 additions and 13 deletions.
76 changes: 76 additions & 0 deletions main/controllers/AdminController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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
})
const admin = await doc.save();
console.log("here1")
const token = jwt.sign({
_id: admin._id,
},
'secret2314',
{
expiresIn: '30d',
}
);
console.log("here2")
const {passwordHash, ...AdminData} = admin._doc;

console.log("here3")
res.json({
...AdminData,
token
});
} catch (err) {
res.status(500).json({
message: "Не удалось авторизоваться"
})
}
}

export const login = async (req, res) => {
try {
const msg = "Неверный логин или пароль";
const admin = await AdminModel.findOne({login: req.body.login});
if (!admin) {
return res.status(404).json({
message: msg
})
}
// Если отсюда убрать await, то, зная логин, можно войти с любым паролем! Лол)
const isValidPassword = await bcrypt.compare(req.body.password, admin._doc.passwordHash);
if (!isValidPassword) {
return res.status(404).json({
message: msg
})
}
const token = jwt.sign({
_id: admin._id,
},
'secret2314',
{
expiresIn: '30d',
});
const { passwordHash, ...AdminData } = admin._doc;
res.json({
...AdminData,
token
});
}catch (err) {
console.warn(err);
res.status(500).json({
message: "Не удалось авторизоваться"
})
}
}

43 changes: 43 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import express from 'express';
import mongoose from "mongoose";
import cors from "cors";
import {login, register} from "./controllers/AdminController.js";
import checkAuth from "./utils/checkAuth.js";
const app = express();

const PORT = 4444;
const __dirname = import.meta.dirname;

mongoose.connect("mongodb://127.0.0.1:27017/build_pc")
.then(() => console.log('DB ok'))
.catch((err) => console.warn('DB error: ', err));

app.use(cors())
app.use(express.json());
app.use(express.static(__dirname + '/_front'));


app.get('/', (req, res) => {
res.send("Hello world!");
});

app.post('/auth/register', register);

app.post('/auth/login', login);


app.listen(PORT, (err) => {
if (err) {
console.warn(err)
} else {
console.log(`Server successfully started at port ${PORT}`);
}
});

/*
На данный момент
{
"login": "admin",
"password": "12345"
}
*/
14 changes: 14 additions & 0 deletions main/models/Admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mongoose from "mongoose";

const AdminSchema = new mongoose.Schema({
login: {
type: String,
required: true,
},
passwordHash: {
type: String,
required: true,
}
}, { timestamps: true });

export default mongoose.model("Admin", AdminSchema);
Loading

0 comments on commit dd49aad

Please sign in to comment.