From 6f4103551a33fab9692038e8222262cabc47c26b Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Fri, 3 Jan 2020 17:59:02 +0300 Subject: [PATCH] administratot component --- config/routes/admin.js | 25 ++------------- config/routes/control-panel.js | 24 ++++++++++++++ dist/views/admin/administrators.pug | 27 +++++++++++++++- dist/views/admin/assets/templates/header.pug | 8 ++--- .../components/administrator.component.js | 31 +++++++++++++++++++ src/admin/index.js | 2 ++ 6 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 src/admin/components/administrator.component.js diff --git a/config/routes/admin.js b/config/routes/admin.js index f2b5994..3b94ca2 100644 --- a/config/routes/admin.js +++ b/config/routes/admin.js @@ -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'); - }) + }); } \ No newline at end of file diff --git a/config/routes/control-panel.js b/config/routes/control-panel.js index 880d106..64f8fc0 100644 --- a/config/routes/control-panel.js +++ b/config/routes/control-panel.js @@ -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'); + } + }) } \ No newline at end of file diff --git a/dist/views/admin/administrators.pug b/dist/views/admin/administrators.pug index 6e3ae48..c947df2 100644 --- a/dist/views/admin/administrators.pug +++ b/dist/views/admin/administrators.pug @@ -5,7 +5,32 @@ body.has-fixed-sidenav .container .row .col.s12 - h2 Редактирование администраторов + 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 \ No newline at end of file diff --git a/dist/views/admin/assets/templates/header.pug b/dist/views/admin/assets/templates/header.pug index 438a6f5..e40f502 100644 --- a/dist/views/admin/assets/templates/header.pug +++ b/dist/views/admin/assets/templates/header.pug @@ -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 diff --git a/src/admin/components/administrator.component.js b/src/admin/components/administrator.component.js new file mode 100644 index 0000000..df8af89 --- /dev/null +++ b/src/admin/components/administrator.component.js @@ -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('Невозможно выполнить запрос.'); + } + } +} \ No newline at end of file diff --git a/src/admin/index.js b/src/admin/index.js index 159a825..2c5e365 100644 --- a/src/admin/index.js +++ b/src/admin/index.js @@ -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');