From 6c24660bbbf48a286d804547423ff46408aeafc2 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 23 May 2020 12:40:01 -0700 Subject: [PATCH] added auth check to settings updating, moved settings sync to setting data class --- brain/api/v1/settings.js | 64 ++++++++-------------------- brain/data/Settings.js | 62 +++++++++++++++++++++++++++ src/com/actions/SettingsActions.js | 22 ++-------- src/com/controllers/SettingsIndex.js | 24 +++++++---- src/com/utils/APIUtils.js | 1 + 5 files changed, 99 insertions(+), 74 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 471630d..07433bd 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -1,6 +1,7 @@ import * as DataEvent from '../../../src/com/events/DataEvent'; import Auth from '../../data/Auth'; import Render from '../../data/Render'; +import SettingsData from '../../data/Settings'; import Book from '../../data/Book'; const express = require('express'); const router = express.Router(); @@ -13,6 +14,7 @@ const folks = require('../../../site/folks.json'); const auth = new Auth(); const render = new Render(); const book = new Book(); +const settingsData = new SettingsData(); const uploadPath = './public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM'); fs.ensureDir(uploadPath, () => { @@ -35,48 +37,10 @@ var background_upload = multer({ }).array('background_upload'); //** SYNC POSTS */ router.post('/sync', (req, res) => { - let payload = req.body; - if (req.session.user) { - let user = req.session.user; - let found = _.find(folks, { id: user.id }); - let needToUpdate = false; - if (found) { - let index = found.id - 1; - if ( - folks[index].handle != payload.member.handle || - folks[index].email != payload.member.email - ) { - folks[index].handle = payload.member.handle; - folks[index].email = payload.member.email; - fs.writeJson('site/folks.json', folks); - } else { - //no need to save - } - } else { - res.json({ - type: DataEvent.REQUEST_LAME, - message: "You're not logged in, champ" - }); - } - - if (!_.isEqual(settings.global, payload.global)) { - let bg = payload.global.background; - payload.global.background = bg.substr(21, bg.length); - settings.global = payload.global; - needToUpdate = true; - } else { - //no need to save - } - - if (!_.isEqual(settings.email, payload.email)) { - settings.email = payload.email; - needToUpdate = true; - } else { - //no need to save - } - - if (needToUpdate) { - fs.writeJson('site/settings.json', settings) + auth.authCheck(req) + .then(() => { + settingsData + .sync(req, res) .then(() => { res.json({ type: DataEvent.SETTINGS_UPDATED, @@ -84,12 +48,18 @@ router.post('/sync', (req, res) => { }); }) .catch(() => { - //console.error(err); + res.json({ + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" + }); }); - } else { - //no need to update - } - } + }) + .catch(err => { + res.json({ + type: err.type, + message: err.message + }); + }); }); router.post('/nav-sync', (req, res) => { diff --git a/brain/data/Settings.js b/brain/data/Settings.js index c7a4fbd..c769fee 100644 --- a/brain/data/Settings.js +++ b/brain/data/Settings.js @@ -1,5 +1,7 @@ import * as DataEvent from '../../src/com/events/DataEvent'; import fs from 'fs-extra'; +const settings = require('../../site/settings.json'); +const folks = require('../../site/folks.json'); const config = require('../../site/tags.json'); const _ = require('lodash'); @@ -11,6 +13,66 @@ export default class Settings { //-------------------------- // methods //-------------------------- + sync(req) { + return new Promise((resolve, reject) => { + let payload = req.body; + let user = req.session.user; + let found = _.find(folks, { id: user.id }); + let needToUpdate = false; + let response = []; + if (found) { + let index = found.id - 1; + if ( + folks[index].handle != payload.member.handle || + folks[index].email != payload.member.email + ) { + folks[index].handle = payload.member.handle; + folks[index].email = payload.member.email; + fs.writeJson('site/folks.json', folks); + } else { + //no need to save + } + } else { + let response = { + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" + }; + reject(response); + } + + if (!_.isEqual(settings.global, payload.global)) { + let bg = payload.global.background; + payload.global.background = bg.substr(21, bg.length); + settings.global = payload.global; + needToUpdate = true; + } else { + //no need to save + } + + if (!_.isEqual(settings.email, payload.email)) { + settings.email = payload.email; + needToUpdate = true; + } else { + //no need to save + } + + if (needToUpdate) { + fs.writeJson('site/settings.json', settings) + .then(() => { + response = { + type: DataEvent.SETTINGS_UPDATED, + message: 'Settings Saved' + }; + resolve(response); + }) + .catch(() => { + //console.error(err); + }); + } else { + //no need to update + } + }); + } saveTags(tags) { return new Promise((resolve, reject) => { if (!_.isEqual(config.tags, tags)) { diff --git a/src/com/actions/SettingsActions.js b/src/com/actions/SettingsActions.js index c144268..eb81fdf 100644 --- a/src/com/actions/SettingsActions.js +++ b/src/com/actions/SettingsActions.js @@ -1,6 +1,3 @@ -import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils'; -import * as DataEvent from '../../../src/com/events/DataEvent'; -const api = new ApiUtils(); export default class SettingsActions { //-------------------------- // constructor @@ -9,7 +6,7 @@ export default class SettingsActions { //-------------------------- // methods //-------------------------- - save() { + getInfo() { let handle = document.getElementById('settings-handle').value; let email = document.getElementById('settings-email').value; let url = document.getElementById('settings-url').value; @@ -60,21 +57,8 @@ export default class SettingsActions { } } }; - return new Promise(function(resolve, reject) { - api.request( - '/api/v1/settings/sync', - DataEvent.SETTINGS_UPDATED, - REQUEST_TYPE_POST, - CONTENT_TYPE_JSON, - settingsData - ) - .then(response => { - //console.log("RESPONSE", response) - resolve(response); - }) - .catch(err => { - reject(err); - }); + return new Promise(function (resolve) { + resolve(settingsData); }); } //-------------------------- diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 88c524a..2e55169 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -26,14 +26,22 @@ export default class SettingsIndex { //handle save button document.getElementById('save-toggle').addEventListener('click', () => new SettingsActions() - .save() - .then(response => { - let r = JSON.parse(response.request['response']); - if (r.type == DataEvent.SETTINGS_UPDATED) { - notify.alert(r.message, true); - } else { - notify.alert(r.message, true); - } + .getInfo() + .then(data => { + api.request( + '/api/v1/settings/sync', + DataEvent.API_SETTINGS_WRITE, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ).then(response => { + let r = JSON.parse(response.request['response']); + if (r.type == DataEvent.SETTINGS_UPDATED) { + notify.alert(r.message, true); + } else { + notify.alert(r.message, true); + } + }); }) .catch(() => { //console.log(err); diff --git a/src/com/utils/APIUtils.js b/src/com/utils/APIUtils.js index 09ee79e..6dc74a0 100644 --- a/src/com/utils/APIUtils.js +++ b/src/com/utils/APIUtils.js @@ -60,6 +60,7 @@ export default class APIUtils { eventType === DataEvent.API_RENDER_PAGES ) request.setRequestHeader('x-access-token', self.token); + switch (contentType) { case CONTENT_TYPE_JSON: request.setRequestHeader('Content-type', 'application/' + contentType);