generated from moevm/nsql-clean-tempate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from moevm/bogdanov_admin
Server: Admin
- Loading branch information
Showing
6 changed files
with
841 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "Не удалось авторизоваться" | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.