added auth check to settings updating, moved settings sync to setting data class
This commit is contained in:
parent
161f441b84
commit
6c24660bbb
5 changed files with 99 additions and 74 deletions
|
@ -1,6 +1,7 @@
|
||||||
import * as DataEvent from '../../../src/com/events/DataEvent';
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
||||||
import Auth from '../../data/Auth';
|
import Auth from '../../data/Auth';
|
||||||
import Render from '../../data/Render';
|
import Render from '../../data/Render';
|
||||||
|
import SettingsData from '../../data/Settings';
|
||||||
import Book from '../../data/Book';
|
import Book from '../../data/Book';
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
@ -13,6 +14,7 @@ const folks = require('../../../site/folks.json');
|
||||||
const auth = new Auth();
|
const auth = new Auth();
|
||||||
const render = new Render();
|
const render = new Render();
|
||||||
const book = new Book();
|
const book = new Book();
|
||||||
|
const settingsData = new SettingsData();
|
||||||
const uploadPath =
|
const uploadPath =
|
||||||
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
|
'./public/assets/images/user/' + moment().format('YYYY') + '/' + moment().format('MM');
|
||||||
fs.ensureDir(uploadPath, () => {
|
fs.ensureDir(uploadPath, () => {
|
||||||
|
@ -35,48 +37,10 @@ var background_upload = multer({
|
||||||
}).array('background_upload');
|
}).array('background_upload');
|
||||||
//** SYNC POSTS */
|
//** SYNC POSTS */
|
||||||
router.post('/sync', (req, res) => {
|
router.post('/sync', (req, res) => {
|
||||||
let payload = req.body;
|
auth.authCheck(req)
|
||||||
if (req.session.user) {
|
.then(() => {
|
||||||
let user = req.session.user;
|
settingsData
|
||||||
let found = _.find(folks, { id: user.id });
|
.sync(req, res)
|
||||||
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)
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
res.json({
|
res.json({
|
||||||
type: DataEvent.SETTINGS_UPDATED,
|
type: DataEvent.SETTINGS_UPDATED,
|
||||||
|
@ -84,12 +48,18 @@ router.post('/sync', (req, res) => {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.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) => {
|
router.post('/nav-sync', (req, res) => {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import * as DataEvent from '../../src/com/events/DataEvent';
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
|
const settings = require('../../site/settings.json');
|
||||||
|
const folks = require('../../site/folks.json');
|
||||||
const config = require('../../site/tags.json');
|
const config = require('../../site/tags.json');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
@ -11,6 +13,66 @@ export default class Settings {
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// methods
|
// 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) {
|
saveTags(tags) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!_.isEqual(config.tags, tags)) {
|
if (!_.isEqual(config.tags, tags)) {
|
||||||
|
|
|
@ -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 {
|
export default class SettingsActions {
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// constructor
|
// constructor
|
||||||
|
@ -9,7 +6,7 @@ export default class SettingsActions {
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// methods
|
// methods
|
||||||
//--------------------------
|
//--------------------------
|
||||||
save() {
|
getInfo() {
|
||||||
let handle = document.getElementById('settings-handle').value;
|
let handle = document.getElementById('settings-handle').value;
|
||||||
let email = document.getElementById('settings-email').value;
|
let email = document.getElementById('settings-email').value;
|
||||||
let url = document.getElementById('settings-url').value;
|
let url = document.getElementById('settings-url').value;
|
||||||
|
@ -60,21 +57,8 @@ export default class SettingsActions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function (resolve) {
|
||||||
api.request(
|
resolve(settingsData);
|
||||||
'/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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
//--------------------------
|
//--------------------------
|
||||||
|
|
|
@ -26,14 +26,22 @@ export default class SettingsIndex {
|
||||||
//handle save button
|
//handle save button
|
||||||
document.getElementById('save-toggle').addEventListener('click', () =>
|
document.getElementById('save-toggle').addEventListener('click', () =>
|
||||||
new SettingsActions()
|
new SettingsActions()
|
||||||
.save()
|
.getInfo()
|
||||||
.then(response => {
|
.then(data => {
|
||||||
let r = JSON.parse(response.request['response']);
|
api.request(
|
||||||
if (r.type == DataEvent.SETTINGS_UPDATED) {
|
'/api/v1/settings/sync',
|
||||||
notify.alert(r.message, true);
|
DataEvent.API_SETTINGS_WRITE,
|
||||||
} else {
|
REQUEST_TYPE_POST,
|
||||||
notify.alert(r.message, true);
|
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(() => {
|
.catch(() => {
|
||||||
//console.log(err);
|
//console.log(err);
|
||||||
|
|
|
@ -60,6 +60,7 @@ export default class APIUtils {
|
||||||
eventType === DataEvent.API_RENDER_PAGES
|
eventType === DataEvent.API_RENDER_PAGES
|
||||||
)
|
)
|
||||||
request.setRequestHeader('x-access-token', self.token);
|
request.setRequestHeader('x-access-token', self.token);
|
||||||
|
|
||||||
switch (contentType) {
|
switch (contentType) {
|
||||||
case CONTENT_TYPE_JSON:
|
case CONTENT_TYPE_JSON:
|
||||||
request.setRequestHeader('Content-type', 'application/' + contentType);
|
request.setRequestHeader('Content-type', 'application/' + contentType);
|
||||||
|
|
Loading…
Reference in a new issue