2015-05-15 00:58:13 -04:00
|
|
|
//common
|
2016-04-21 16:38:43 -04:00
|
|
|
var domain = ''; // domain name
|
2016-02-16 23:08:44 -05:00
|
|
|
var urlpath = ''; // sub url path, like: www.example.com/<urlpath>
|
2016-04-20 06:20:43 -04:00
|
|
|
//settings
|
|
|
|
var debug = false;
|
2016-02-16 23:08:44 -05:00
|
|
|
|
2016-05-14 22:54:24 -04:00
|
|
|
var GOOGLE_API_KEY = '';
|
|
|
|
var GOOGLE_CLIENT_ID = '';
|
|
|
|
|
|
|
|
var DROPBOX_APP_KEY = '';
|
2016-03-04 10:17:35 -05:00
|
|
|
|
2016-02-16 23:08:44 -05:00
|
|
|
var port = window.location.port;
|
2016-04-21 16:38:43 -04:00
|
|
|
var serverurl = window.location.protocol + '//' + (domain ? domain : window.location.hostname) + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : '');
|
2016-02-16 23:08:44 -05:00
|
|
|
var noteid = urlpath ? window.location.pathname.slice(urlpath.length + 1, window.location.pathname.length).split('/')[1] : window.location.pathname.split('/')[1];
|
|
|
|
var noteurl = serverurl + '/' + noteid;
|
|
|
|
|
2016-06-27 21:38:31 -04:00
|
|
|
var version = '0.4.3';
|
2016-04-20 06:20:43 -04:00
|
|
|
|
2015-05-15 00:58:13 -04:00
|
|
|
var checkAuth = false;
|
|
|
|
var profile = null;
|
|
|
|
var lastLoginState = getLoginState();
|
2015-06-01 06:04:25 -04:00
|
|
|
var lastUserId = getUserId();
|
2015-05-15 00:58:13 -04:00
|
|
|
var loginStateChangeEvent = null;
|
|
|
|
|
|
|
|
function resetCheckAuth() {
|
|
|
|
checkAuth = false;
|
|
|
|
}
|
|
|
|
|
2015-06-01 06:04:25 -04:00
|
|
|
function setLoginState(bool, id) {
|
2015-05-15 00:58:13 -04:00
|
|
|
Cookies.set('loginstate', bool, {
|
2016-06-26 11:37:59 -04:00
|
|
|
expires: 365
|
2015-05-15 00:58:13 -04:00
|
|
|
});
|
2015-06-01 06:04:25 -04:00
|
|
|
if (id) {
|
|
|
|
Cookies.set('userid', id, {
|
2016-06-26 11:37:59 -04:00
|
|
|
expires: 365
|
2015-06-01 06:04:25 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Cookies.remove('userid');
|
|
|
|
}
|
2015-05-15 00:58:13 -04:00
|
|
|
lastLoginState = bool;
|
2015-06-01 06:04:25 -04:00
|
|
|
lastUserId = id;
|
|
|
|
checkLoginStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLoginStateChanged() {
|
|
|
|
if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
|
|
|
|
if(loginStateChangeEvent)
|
|
|
|
loginStateChangeEvent();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-15 00:58:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLoginState() {
|
2016-07-29 22:59:51 -04:00
|
|
|
var state = Cookies.get('loginstate');
|
|
|
|
return state === "true" || state === true;
|
2015-05-15 00:58:13 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 06:04:25 -04:00
|
|
|
function getUserId() {
|
|
|
|
return Cookies.get('userid');
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:58:13 -04:00
|
|
|
function clearLoginState() {
|
|
|
|
Cookies.remove('loginstate');
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkIfAuth(yesCallback, noCallback) {
|
|
|
|
var cookieLoginState = getLoginState();
|
2015-06-01 06:04:25 -04:00
|
|
|
if (checkLoginStateChanged())
|
|
|
|
checkAuth = false;
|
2015-05-15 00:58:13 -04:00
|
|
|
if (!checkAuth || typeof cookieLoginState == 'undefined') {
|
2016-02-16 23:08:44 -05:00
|
|
|
$.get(serverurl + '/me')
|
2015-05-15 00:58:13 -04:00
|
|
|
.done(function (data) {
|
|
|
|
if (data && data.status == 'ok') {
|
|
|
|
profile = data;
|
|
|
|
yesCallback(profile);
|
2015-06-01 06:04:25 -04:00
|
|
|
setLoginState(true, data.id);
|
2015-05-15 00:58:13 -04:00
|
|
|
} else {
|
|
|
|
noCallback();
|
|
|
|
setLoginState(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail(function () {
|
|
|
|
noCallback();
|
|
|
|
setLoginState(false);
|
2015-06-01 06:04:25 -04:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
checkAuth = true;
|
2015-05-15 00:58:13 -04:00
|
|
|
});
|
|
|
|
} else if (cookieLoginState) {
|
|
|
|
yesCallback(profile);
|
|
|
|
} else {
|
|
|
|
noCallback();
|
|
|
|
}
|
|
|
|
}
|