134 lines
4.9 KiB
JavaScript
134 lines
4.9 KiB
JavaScript
$(document).ready(function() {
|
|
const $moonIcon = $('#moon-icon');
|
|
const $sunIcon = $('#sun-icon');
|
|
$('#theme-toggle').on('click', function() {
|
|
$('body').toggleClass('theme-light theme-dark');
|
|
// Switch icon based on current theme
|
|
const $themeIcon = $('#theme-icon');
|
|
if ($('body').hasClass('theme-light')) {
|
|
localStorage.setItem("theme", "light")
|
|
$themeIcon.html($('#sun-icon').outerHTML); // Set sun icon
|
|
$moonIcon.hide(); // Hide moon icon
|
|
$sunIcon.show(); // Show sun icon
|
|
} else {
|
|
localStorage.setItem("theme", "dark")
|
|
$themeIcon.html($('#moon-icon').outerHTML); // Set moon icon
|
|
$moonIcon.show(); // Show moon icon
|
|
$sunIcon.hide(); // Hide sun icon
|
|
}
|
|
});
|
|
|
|
$('.logout-button').on('click', function() {
|
|
localStorage.removeItem("token")
|
|
window.location.href = removeURLParameter(window.location.href, "token");
|
|
})
|
|
|
|
let params = new URLSearchParams(window.location.search);
|
|
const theme = params.get('theme');
|
|
applyTheme(theme);
|
|
if (params.has("callback")) {
|
|
checkToken(localStorage.getItem("token"), true)
|
|
localStorage.setItem("callback", params.get("callback").replaceAll("\"", ""));
|
|
} else if(params.has("token")) {
|
|
callback = localStorage.getItem("callback")
|
|
localStorage.removeItem("callback")
|
|
localStorage.setItem("token", params.get("token"))
|
|
if (callback != null) window.location.href = decodeURI(callback).replaceAll("TOKEN", params.get("token"));
|
|
else checkToken(localStorage.getItem("token"), false)
|
|
} else {
|
|
checkToken(localStorage.getItem("token"), false)
|
|
}
|
|
});
|
|
|
|
function checkToken(token, redir) {
|
|
console.log("Checking token...")
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('GET', 'https://auth.easystartup.su/api/user/self', true);
|
|
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
xhr.onload = function() {
|
|
if(xhr.status == 200) {
|
|
if (redir) {
|
|
localStorage.setItem("token", token)
|
|
callback = localStorage.getItem("callback")
|
|
localStorage.removeItem("callback")
|
|
if (callback != null) window.location.href = decodeURI(callback).replaceAll("TOKEN", localStorage.getItem("token"));
|
|
else {
|
|
let ans = JSON.parse(xhr.responseText)
|
|
if (ans.google_id != "") {
|
|
$('#google-button').hide()
|
|
$('.logout-button').css("visibility", "visible");
|
|
}
|
|
if (ans.telegram_id != "") {
|
|
$("iframe").hide()
|
|
$('.logout-button').css("visibility", "visible");
|
|
}
|
|
|
|
if((ans.google_id != "") && (ans.telegram_id != "")) {
|
|
$("#subtitle").hide()
|
|
}
|
|
}
|
|
} else {
|
|
let ans = JSON.parse(xhr.responseText)
|
|
if (ans.google_id != "") {
|
|
$('#google-button').hide()
|
|
$('.logout-button').css("visibility", "visible");
|
|
}
|
|
if (ans.telegram_id != "") {
|
|
$("iframe").hide()
|
|
$('.logout-button').css("visibility", "visible");
|
|
}
|
|
if((ans.google_id != "") && (ans.telegram_id != "")) {
|
|
$("#subtitle").hide()
|
|
}
|
|
}
|
|
}
|
|
};
|
|
xhr.onerror = function() {
|
|
console.log('Network error');
|
|
};
|
|
xhr.send();
|
|
}
|
|
|
|
|
|
|
|
function applyTheme(theme) {
|
|
const body = document.body;
|
|
const $moonIcon = $('#moon-icon');
|
|
const $sunIcon = $('#sun-icon');
|
|
body.classList.remove('theme-light', 'theme-dark');
|
|
if (theme == null) theme = localStorage.getItem("theme")
|
|
if (theme === 'light') {
|
|
$moonIcon.hide();
|
|
$sunIcon.show();
|
|
body.classList.add('theme-light');
|
|
} else {
|
|
$moonIcon.show();
|
|
$sunIcon.hide();
|
|
theme = "dark"
|
|
body.classList.add('theme-dark');
|
|
}
|
|
theme = localStorage.setItem("theme", theme)
|
|
}
|
|
|
|
|
|
function removeURLParameter(url, parameter) {
|
|
//prefer to use l.search if you have a location/link object
|
|
var urlparts = url.split('?');
|
|
if (urlparts.length >= 2) {
|
|
|
|
var prefix = encodeURIComponent(parameter) + '=';
|
|
var pars = urlparts[1].split(/[&;]/g);
|
|
|
|
//reverse iteration as may be destructive
|
|
for (var i = pars.length; i-- > 0;) {
|
|
//idiom for string.startsWith
|
|
if (pars[i].lastIndexOf(prefix, 0) !== -1) {
|
|
pars.splice(i, 1);
|
|
}
|
|
}
|
|
|
|
return urlparts[0] + (pars.length > 0 ? '?' + pars.join('&') : '');
|
|
}
|
|
return url;
|
|
} |