hedgedoc/public/js/common.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-05-15 00:58:13 -04:00
//common
var domain = 'change this';
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, {
expires: 14
});
2015-06-01 06:04:25 -04:00
if (id) {
Cookies.set('userid', id, {
expires: 14
});
} 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() {
return Cookies.get('loginstate') === "true";
}
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') {
$.get('/me')
.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();
}
}