2015-05-15 04:58:13 +00:00
|
|
|
//common
|
2016-02-17 04:08:44 +00:00
|
|
|
var domain = 'change this'; // domain name
|
|
|
|
var urlpath = ''; // sub url path, like: www.example.com/<urlpath>
|
2016-04-20 10:20:43 +00:00
|
|
|
//settings
|
|
|
|
var debug = false;
|
2016-02-17 04:08:44 +00:00
|
|
|
|
2016-03-04 15:17:35 +00:00
|
|
|
var GOOGLE_API_KEY = 'change this';
|
|
|
|
var GOOGLE_CLIENT_ID = 'change this';
|
|
|
|
|
2016-02-17 04:08:44 +00:00
|
|
|
var port = window.location.port;
|
|
|
|
var serverurl = window.location.protocol + '//' + domain + (port ? ':' + port : '') + (urlpath ? '/' + urlpath : '');
|
|
|
|
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-04-20 10:23:56 +00:00
|
|
|
var version = '0.4.0';
|
2016-04-20 10:20:43 +00:00
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
var checkAuth = false;
|
|
|
|
var profile = null;
|
|
|
|
var lastLoginState = getLoginState();
|
2015-06-01 10:04:25 +00:00
|
|
|
var lastUserId = getUserId();
|
2015-05-15 04:58:13 +00:00
|
|
|
var loginStateChangeEvent = null;
|
|
|
|
|
|
|
|
function resetCheckAuth() {
|
|
|
|
checkAuth = false;
|
|
|
|
}
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
function setLoginState(bool, id) {
|
2015-05-15 04:58:13 +00:00
|
|
|
Cookies.set('loginstate', bool, {
|
|
|
|
expires: 14
|
|
|
|
});
|
2015-06-01 10:04:25 +00:00
|
|
|
if (id) {
|
|
|
|
Cookies.set('userid', id, {
|
|
|
|
expires: 14
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Cookies.remove('userid');
|
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
lastLoginState = bool;
|
2015-06-01 10:04:25 +00:00
|
|
|
lastUserId = id;
|
|
|
|
checkLoginStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLoginStateChanged() {
|
|
|
|
if (getLoginState() != lastLoginState || getUserId() != lastUserId) {
|
|
|
|
if(loginStateChangeEvent)
|
|
|
|
loginStateChangeEvent();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-15 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLoginState() {
|
|
|
|
return Cookies.get('loginstate') === "true";
|
|
|
|
}
|
|
|
|
|
2015-06-01 10:04:25 +00:00
|
|
|
function getUserId() {
|
|
|
|
return Cookies.get('userid');
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:58:13 +00:00
|
|
|
function clearLoginState() {
|
|
|
|
Cookies.remove('loginstate');
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkIfAuth(yesCallback, noCallback) {
|
|
|
|
var cookieLoginState = getLoginState();
|
2015-06-01 10:04:25 +00:00
|
|
|
if (checkLoginStateChanged())
|
|
|
|
checkAuth = false;
|
2015-05-15 04:58:13 +00:00
|
|
|
if (!checkAuth || typeof cookieLoginState == 'undefined') {
|
2016-02-17 04:08:44 +00:00
|
|
|
$.get(serverurl + '/me')
|
2015-05-15 04:58:13 +00:00
|
|
|
.done(function (data) {
|
|
|
|
if (data && data.status == 'ok') {
|
|
|
|
profile = data;
|
|
|
|
yesCallback(profile);
|
2015-06-01 10:04:25 +00:00
|
|
|
setLoginState(true, data.id);
|
2015-05-15 04:58:13 +00:00
|
|
|
} else {
|
|
|
|
noCallback();
|
|
|
|
setLoginState(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail(function () {
|
|
|
|
noCallback();
|
|
|
|
setLoginState(false);
|
2015-06-01 10:04:25 +00:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
checkAuth = true;
|
2015-05-15 04:58:13 +00:00
|
|
|
});
|
|
|
|
} else if (cookieLoginState) {
|
|
|
|
yesCallback(profile);
|
|
|
|
} else {
|
|
|
|
noCallback();
|
|
|
|
}
|
|
|
|
}
|