From c2a8911b9c7872ea0d085020aa17e82162130f3d Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 22:46:38 +0800 Subject: Move config variable to lib/config --- public/js/common.js | 25 +------------------------ public/js/lib/config/index.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 24 deletions(-) create mode 100644 public/js/lib/config/index.js (limited to 'public/js') diff --git a/public/js/common.js b/public/js/common.js index 6d54b450..9a60122b 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -1,21 +1,4 @@ -// import config from './config'; - -import { - domain, // domain name - urlpath, // sub url path, like: www.example.com/ - debug, - GOOGLE_API_KEY, - GOOGLE_CLIENT_ID, - DROPBOX_APP_KEY -} from './config'; - -//common -export const port = window.location.port; -window.serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`; -export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]; -export const noteurl = `${serverurl}/${noteid}`; - -export const version = '0.5.0'; +import { serverurl } from './lib/config'; let checkAuth = false; let profile = null; @@ -101,12 +84,6 @@ export function checkIfAuth(yesCallback, noCallback) { } export default { - domain, - urlpath, - debug, - GOOGLE_API_KEY, - GOOGLE_CLIENT_ID, - DROPBOX_APP_KEY, checkAuth, profile, lastLoginState, diff --git a/public/js/lib/config/index.js b/public/js/lib/config/index.js new file mode 100644 index 00000000..bffbadd9 --- /dev/null +++ b/public/js/lib/config/index.js @@ -0,0 +1,19 @@ +import configJson from '../../../../config.json'; // root path json config + +const config = 'production' === process.env.NODE_ENV ? configJson.production : configJson.development; + +export const GOOGLE_API_KEY = config.google && config.google.apiKey; +export const GOOGLE_CLIENT_ID = config.google && config.google.clientID +export const DROPBOX_APP_KEY = config.dropbox && config.dropbox.appKey + +export const domain = config.domain; +export const urlpath = config.urlpath; +export const debug = config.debug; + +export const port = window.location.port; +export const serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`; +window.serverurl = serverurl; +export const noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1]; +export const noteurl = `${serverurl}/${noteid}`; + +export const version = '0.5.0'; -- cgit v1.2.3 From 0fca629c34b83617b2d72e42aa0edb66fd2e71f6 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 22:51:44 +0800 Subject: Rename common.js to login.js --- public/js/common.js | 92 ------------------------------------------- public/js/cover.js | 2 +- public/js/extra.js | 2 +- public/js/history.js | 8 +++- public/js/index.js | 7 +++- public/js/lib/common/login.js | 92 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 98 deletions(-) delete mode 100644 public/js/common.js create mode 100644 public/js/lib/common/login.js (limited to 'public/js') diff --git a/public/js/common.js b/public/js/common.js deleted file mode 100644 index 9a60122b..00000000 --- a/public/js/common.js +++ /dev/null @@ -1,92 +0,0 @@ -import { serverurl } from './lib/config'; - -let checkAuth = false; -let profile = null; -let lastLoginState = getLoginState(); -let lastUserId = getUserId(); -let loginStateChangeEvent = null; - -export function setloginStateChangeEvent(func) { - loginStateChangeEvent = func; -} - -export function resetCheckAuth() { - checkAuth = false; -} - -export function setLoginState(bool, id) { - Cookies.set('loginstate', bool, { - expires: 365 - }); - if (id) { - Cookies.set('userid', id, { - expires: 365 - }); - } else { - Cookies.remove('userid'); - } - lastLoginState = bool; - lastUserId = id; - checkLoginStateChanged(); -} - -export function checkLoginStateChanged() { - if (getLoginState() != lastLoginState || getUserId() != lastUserId) { - if(loginStateChangeEvent) { - loginStateChangeEvent(); - } - return true; - } else { - return false; - } -} - -export function getLoginState() { - const state = Cookies.get('loginstate'); - return state === "true" || state === true; -} - -export function getUserId() { - return Cookies.get('userid'); -} - -export function clearLoginState() { - Cookies.remove('loginstate'); -} - -export function checkIfAuth(yesCallback, noCallback) { - const cookieLoginState = getLoginState(); - if (checkLoginStateChanged()) - checkAuth = false; - if (!checkAuth || typeof cookieLoginState == 'undefined') { - $.get(`${serverurl}/me`) - .done(data => { - if (data && data.status == 'ok') { - profile = data; - yesCallback(profile); - setLoginState(true, data.id); - } else { - noCallback(); - setLoginState(false); - } - }) - .fail(() => { - noCallback(); - }) - .always(() => { - checkAuth = true; - }); - } else if (cookieLoginState) { - yesCallback(profile); - } else { - noCallback(); - } -} - -export default { - checkAuth, - profile, - lastLoginState, - lastUserId, - loginStateChangeEvent -}; diff --git a/public/js/cover.js b/public/js/cover.js index 677d82eb..bc04923b 100644 --- a/public/js/cover.js +++ b/public/js/cover.js @@ -9,7 +9,7 @@ import { getLoginState, resetCheckAuth, setloginStateChangeEvent -} from './common'; +} from './lib/common/login'; import { clearDuplicatedHistory, diff --git a/public/js/extra.js b/public/js/extra.js index 6cfb5b0a..b651d9e6 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -11,7 +11,7 @@ import PDFObject from 'pdfobject'; import S from 'string'; import { saveAs } from 'file-saver'; -require('./common'); +require('./lib/common/login'); require('../vendor/md-toc'); var Viz = require("viz.js"); diff --git a/public/js/history.js b/public/js/history.js index f1201683..34b2cba7 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -1,9 +1,13 @@ import store from 'store'; import S from 'string'; + +import { + checkIfAuth +} from './lib/common/login'; + import { - checkIfAuth, urlpath -} from './common'; +} from './lib/config'; window.migrateHistoryFromTempCallback = null; diff --git a/public/js/index.js b/public/js/index.js index 660f73e4..3bf42ad4 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -19,7 +19,10 @@ var List = require('list.js'); import { checkLoginStateChanged, - setloginStateChangeEvent, + setloginStateChangeEvent +} from './lib/common/login'; + +import { debug, DROPBOX_APP_KEY, GOOGLE_API_KEY, @@ -28,7 +31,7 @@ import { noteurl, urlpath, version -} from './common'; +} from './lib/config'; import { autoLinkify, diff --git a/public/js/lib/common/login.js b/public/js/lib/common/login.js new file mode 100644 index 00000000..12cc41fc --- /dev/null +++ b/public/js/lib/common/login.js @@ -0,0 +1,92 @@ +import { serverurl } from '../config'; + +let checkAuth = false; +let profile = null; +let lastLoginState = getLoginState(); +let lastUserId = getUserId(); +let loginStateChangeEvent = null; + +export function setloginStateChangeEvent(func) { + loginStateChangeEvent = func; +} + +export function resetCheckAuth() { + checkAuth = false; +} + +export function setLoginState(bool, id) { + Cookies.set('loginstate', bool, { + expires: 365 + }); + if (id) { + Cookies.set('userid', id, { + expires: 365 + }); + } else { + Cookies.remove('userid'); + } + lastLoginState = bool; + lastUserId = id; + checkLoginStateChanged(); +} + +export function checkLoginStateChanged() { + if (getLoginState() != lastLoginState || getUserId() != lastUserId) { + if(loginStateChangeEvent) { + loginStateChangeEvent(); + } + return true; + } else { + return false; + } +} + +export function getLoginState() { + const state = Cookies.get('loginstate'); + return state === "true" || state === true; +} + +export function getUserId() { + return Cookies.get('userid'); +} + +export function clearLoginState() { + Cookies.remove('loginstate'); +} + +export function checkIfAuth(yesCallback, noCallback) { + const cookieLoginState = getLoginState(); + if (checkLoginStateChanged()) + checkAuth = false; + if (!checkAuth || typeof cookieLoginState == 'undefined') { + $.get(`${serverurl}/me`) + .done(data => { + if (data && data.status == 'ok') { + profile = data; + yesCallback(profile); + setLoginState(true, data.id); + } else { + noCallback(); + setLoginState(false); + } + }) + .fail(() => { + noCallback(); + }) + .always(() => { + checkAuth = true; + }); + } else if (cookieLoginState) { + yesCallback(profile); + } else { + noCallback(); + } +} + +export default { + checkAuth, + profile, + lastLoginState, + lastUserId, + loginStateChangeEvent +}; -- cgit v1.2.3 From 2408ff4ba96c929a575b34e85d5cff0a01789ca4 Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 23:12:17 +0800 Subject: Add default value for config --- public/js/lib/config/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'public/js') diff --git a/public/js/lib/config/index.js b/public/js/lib/config/index.js index bffbadd9..2b73679f 100644 --- a/public/js/lib/config/index.js +++ b/public/js/lib/config/index.js @@ -2,13 +2,13 @@ import configJson from '../../../../config.json'; // root path json config const config = 'production' === process.env.NODE_ENV ? configJson.production : configJson.development; -export const GOOGLE_API_KEY = config.google && config.google.apiKey; -export const GOOGLE_CLIENT_ID = config.google && config.google.clientID -export const DROPBOX_APP_KEY = config.dropbox && config.dropbox.appKey +export const GOOGLE_API_KEY = (config.google && config.google.apiKey) || ''; +export const GOOGLE_CLIENT_ID = (config.google && config.google.clientID) || ''; +export const DROPBOX_APP_KEY = (config.dropbox && config.dropbox.appKey) || ''; -export const domain = config.domain; -export const urlpath = config.urlpath; -export const debug = config.debug; +export const domain = config.domain || ''; // domain name +export const urlpath = config.urlpath || ''; // sub url path, like: www.example.com/ +export const debug = config.debug || false; export const port = window.location.port; export const serverurl = `${window.location.protocol}//${domain ? domain : window.location.hostname}${port ? ':' + port : ''}${urlpath ? '/' + urlpath : ''}`; -- cgit v1.2.3 From a541569d7edbe827cae69c666079d5d1ded6790c Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Fri, 13 Jan 2017 23:12:27 +0800 Subject: Remove old config.js.example --- public/js/config.js.example | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 public/js/config.js.example (limited to 'public/js') diff --git a/public/js/config.js.example b/public/js/config.js.example deleted file mode 100644 index c5de388f..00000000 --- a/public/js/config.js.example +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - domain: '', // domain name - urlpath: '', // sub url path, like: www.example.com/ - - // settings - debug: false, - - GOOGLE_API_KEY: '', - GOOGLE_CLIENT_ID: '', - DROPBOX_APP_KEY: '' -}; -- cgit v1.2.3 From 04292240d6a589c537afb8e9838dcc0f062d0f2c Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Sat, 14 Jan 2017 15:47:13 +0800 Subject: Minor style update --- public/js/lib/common/login.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public/js') diff --git a/public/js/lib/common/login.js b/public/js/lib/common/login.js index 12cc41fc..f1a03c72 100644 --- a/public/js/lib/common/login.js +++ b/public/js/lib/common/login.js @@ -32,7 +32,7 @@ export function setLoginState(bool, id) { export function checkLoginStateChanged() { if (getLoginState() != lastLoginState || getUserId() != lastUserId) { - if(loginStateChangeEvent) { + if (loginStateChangeEvent) { loginStateChangeEvent(); } return true; -- cgit v1.2.3