added auth check to settings updating, moved settings sync to setting data class

This commit is contained in:
Ro 2020-05-23 12:40:01 -07:00
parent 161f441b84
commit 6c24660bbb
5 changed files with 99 additions and 74 deletions

View file

@ -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) => {

View file

@ -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)) {

View file

@ -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);
});
}
//--------------------------

View file

@ -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);

View file

@ -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);