update google-callback.html, repository.go, script.js, telegram-callback.html

This commit is contained in:
redtea committed 2024-10-23 12:00:00 +03:00
1 parent ecfac68f89
commit d73a4f44cb
4 files changed
+227

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
package repository
import (
"esauth"
"gorm.io/gorm"
)
type Authorization interface {
CreateUser(user *esauth.User) (int64, error)
SaveUser(user *esauth.User) (int64, error)
GetUser(id int64) (esauth.User, error)
FindUser(query, param string) (esauth.User, error)
GetUsers(start, limit int) []int64
DeleteUser(id int64) error
GetUsersCount() int64
}
type Repository struct {
Authorization
}
func NewRepository(db *gorm.DB) *Repository {
return &Repository{
Authorization: NewAuthSchema(db),
}
}
+33
View File
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<script defer>
function bodyLoad() {
if (localStorage.getItem("token") != null) checkToken(localStorage.getItem("token"));
else window.location.href = window.location.href.replace("google-callback", "google-callback-process")
}
function checkToken(token) {
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) {
console.log("Status 200")
let ans = JSON.parse(xhr.responseText)
if(ans.google_id == "") window.location.href = window.location.href.replace("google-callback", "google-callback-process") + "&usertoken=" + token
else window.location.href = window.location.href.replace("google-callback", "google-callback-process")
} else window.location.href = window.location.href.replace("google-callback", "google-callback-process")
};
xhr.onerror = function() {
console.log('Network error');
};
xhr.send();
}
</script>
</head>
<body onload="bodyLoad()"><</body>
+33
View File
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<script defer>
function bodyLoad() {
if (localStorage.getItem("token") != null) checkToken(localStorage.getItem("token"));
else window.location.href = window.location.href.replace("telegram-callback", "telegram-callback-process")
}
function checkToken(token) {
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) {
console.log("Status 200")
let ans = JSON.parse(xhr.responseText)
if(ans.telegram_id == "") window.location.href = window.location.href.replace("telegram-callback", "telegram-callback-process") + "&usertoken=" + token
else window.location.href = window.location.href.replace("telegram-callback", "telegram-callback-process")
} else window.location.href = window.location.href.replace("telegram-callback", "telegram-callback-process")
};
xhr.onerror = function() {
console.log('Network error');
};
xhr.send();
}
</script>
</head>
<body onload="bodyLoad()"><</body>
+134
View File
@@ -0,0 +1,134 @@
$(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;
}