mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
administratot component
This commit is contained in:
parent
835020ca74
commit
6f4103551a
@ -15,10 +15,6 @@ async function loginRoute(req, res, db) {
|
||||
})
|
||||
}
|
||||
|
||||
// function registerRoute(req, res) {
|
||||
// res.render('register')
|
||||
// }
|
||||
|
||||
module.exports = function (server, db) {
|
||||
server.get('/admin', checkNotAuthenticated, (req, res) => {
|
||||
db.collection('administrators').find().toArray((err, result) => {
|
||||
@ -30,30 +26,13 @@ module.exports = function (server, db) {
|
||||
});
|
||||
loginRoute(req, res, db);
|
||||
});
|
||||
// Отдача страницы с регистрацией
|
||||
//server.get('/register', checkNotAuthenticated, (req, res) => registerRoute(req, res));
|
||||
server.post('/admin', checkNotAuthenticated, passport.authenticate('local', {
|
||||
successRedirect: '/control-panel',
|
||||
failureRedirect: '/admin',
|
||||
failureFlash: true
|
||||
}))
|
||||
// Регистрация админа через форму POST
|
||||
// server.post('/register', checkNotAuthenticated, async (req, res) => {
|
||||
// try {
|
||||
// const hashedPassword = await bcrypt.hash(req.body.password, 10);
|
||||
// db.collection('administrators').insertOne({
|
||||
// id: Date.now().toString(),
|
||||
// name: req.body.name,
|
||||
// email: req.body.email,
|
||||
// password: hashedPassword
|
||||
// });
|
||||
// res.redirect('/admin');
|
||||
// } catch {
|
||||
// res.redirect('/register');
|
||||
// }
|
||||
// });
|
||||
}));
|
||||
server.delete('/logout', (req, res) => {
|
||||
req.logOut();
|
||||
res.redirect('/admin');
|
||||
})
|
||||
});
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
const fs = require('fs');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
function checkAuthenticated(req, res, next) {
|
||||
if (req.isAuthenticated()) return next();
|
||||
@ -14,6 +15,7 @@ module.exports = function (server, db) {
|
||||
try {
|
||||
const store = await db.collection('content').findOne({});
|
||||
res.render('admin/control-panel', {
|
||||
id: req.user.id,
|
||||
name: req.user.name,
|
||||
email: req.user.email,
|
||||
title: `${store.title} | Панель управления сайтом`,
|
||||
@ -96,6 +98,7 @@ module.exports = function (server, db) {
|
||||
try {
|
||||
const store = await db.collection('content').findOne({});
|
||||
res.render('admin/administrators', {
|
||||
id: req.user.id,
|
||||
name: req.user.name,
|
||||
email: req.user.email,
|
||||
title: `${store.title} | Администраторы`,
|
||||
@ -254,4 +257,25 @@ module.exports = function (server, db) {
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
|
||||
// администратор
|
||||
server.post('/control-panel/administrators/update', checkAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const data = {
|
||||
id: req.body.id,
|
||||
name: req.body.name,
|
||||
email: req.body.email,
|
||||
oldPass: req.body.oldPass,
|
||||
newPass: req.body.newPass
|
||||
}
|
||||
const admin = await db.collection('administrators').findOne({id: data.id});
|
||||
if(await bcrypt.compare(data.oldPass, admin.password)) {
|
||||
const hashedPassword = await bcrypt.hash(data.newPass, 10);
|
||||
db.collection('administrators').updateOne({id: data.id}, {$set: {name: data.name, email: data.email, password: hashedPassword}});
|
||||
res.send('Данные успешно изменены.');
|
||||
} else res.send('Неверный пароль.');
|
||||
} catch (error) {
|
||||
res.render('main/404');
|
||||
}
|
||||
})
|
||||
}
|
||||
25
dist/views/admin/administrators.pug
vendored
25
dist/views/admin/administrators.pug
vendored
@ -6,6 +6,31 @@ body.has-fixed-sidenav
|
||||
.row
|
||||
.col.s12
|
||||
h2 Редактирование администраторов
|
||||
.col.s12
|
||||
#administrator.card
|
||||
.card-administrator
|
||||
.administrator-update
|
||||
h5 Редактировать данные
|
||||
p Изменяйте данные администратора на этой странице.
|
||||
.row
|
||||
form(action="#")
|
||||
.input-field.col.s12
|
||||
input(style="color:gray;" id="id" type="text" name="id" value=id required readonly)
|
||||
label(for="id") ID Администратора
|
||||
.input-field.col.s12
|
||||
input(id="name" type="text" name="name" value=name required)
|
||||
label(for="name") Имя
|
||||
.input-field.col.s12
|
||||
input(id="email" type="text" name="email" value=email required)
|
||||
label(for="email") Почта
|
||||
.input-field.col.s12
|
||||
input(id="oldPass" type="password" name="oldPass" required)
|
||||
label(for="oldPass") Старый пароль
|
||||
.input-field.col.s12
|
||||
input(id="newPass" type="password" name="newPass" required)
|
||||
label(for="newPass") Новый пароль
|
||||
.edit_success.col.s12
|
||||
button(type="submit" class="btn waves-effect") Сохранить
|
||||
|
||||
include ./assets/templates/footer.pug
|
||||
include ./assets/scripts.pug
|
||||
8
dist/views/admin/assets/templates/header.pug
vendored
8
dist/views/admin/assets/templates/header.pug
vendored
@ -55,10 +55,6 @@ header.admin-header
|
||||
a(href='http://'+host+'/control-panel/content')
|
||||
i.material-icons visibility
|
||||
| Контент на сайте
|
||||
li(data-page="administrators")
|
||||
a(href='http://'+host+'/control-panel/administrators')
|
||||
i.material-icons supervisor_account
|
||||
| Администраторы
|
||||
ul.collapsible
|
||||
li
|
||||
a(href="#" class="collapsible-header waves-effect")
|
||||
@ -66,6 +62,10 @@ header.admin-header
|
||||
| Настройки
|
||||
.collapsible-body
|
||||
ul
|
||||
li(data-page="administrators")
|
||||
a(href='http://'+host+'/control-panel/administrators')
|
||||
i.material-icons supervisor_account
|
||||
| Администратор
|
||||
li(data-page="database")
|
||||
a(href='http://'+host+'/control-panel/database')
|
||||
i.material-icons storage
|
||||
|
||||
31
src/admin/components/administrator.component.js
Normal file
31
src/admin/components/administrator.component.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { Component } from '../core/component';
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
export class AdministratorComponent extends Component {
|
||||
constructor(id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
setElems() {
|
||||
this.$el.querySelector('form').addEventListener('submit', this.request.bind(this));
|
||||
}
|
||||
|
||||
async request(e) {
|
||||
e.preventDefault();
|
||||
try {
|
||||
const data = {
|
||||
id: e.target.elements.id.value,
|
||||
name: e.target.elements.name.value,
|
||||
email: e.target.elements.email.value,
|
||||
oldPass: e.target.elements.oldPass.value,
|
||||
newPass: e.target.elements.newPass.value
|
||||
}
|
||||
const response = await axios.post('/control-panel/administrators/update', data);
|
||||
alert(response.data);
|
||||
document.location.reload(true);
|
||||
} catch (error) {
|
||||
alert('Невозможно выполнить запрос.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ import { ItemEditModal } from "./modals/item-edit.modal.js";
|
||||
import { ItemDeleteModal } from "./modals/item-delete.modal.js";
|
||||
import { CategoryEditModal } from "./modals/category-edit.modal";
|
||||
import { ContentComponent } from "./components/content.component.js";
|
||||
import { AdministratorComponent } from "./components/administrator.component.js";
|
||||
|
||||
// Components
|
||||
const loader = new LoaderComponent('loader');
|
||||
@ -18,6 +19,7 @@ const categories = new CategoriesComponent('categories');
|
||||
const icons = new IconsComponent('icons');
|
||||
const items = new ItemsComponent('items', {pagination}, {search});
|
||||
const content = new ContentComponent('content');
|
||||
const administrator = new AdministratorComponent('administrator');
|
||||
|
||||
// Modals
|
||||
const itemEdit = new ItemEditModal('editItemModal', 'edit', 'editItemClose');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user