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/lib/config/index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 public/js/lib/config/index.js (limited to 'public/js/lib') 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/lib/common/login.js | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 public/js/lib/common/login.js (limited to 'public/js/lib') 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/lib') 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 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/lib') 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