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