From d73a4f44cb3fcc0f5264aaeae16874aeeb5288a1 Mon Sep 17 00:00:00 2001 From: Denis Istratov Date: Wed, 23 Oct 2024 12:00:00 +0300 Subject: [PATCH] update google-callback.html, repository.go, script.js, telegram-callback.html --- pkg/repository/repository.go | 27 ++++++ public/html/google-callback.html | 33 +++++++ public/html/telegram-callback.html | 33 +++++++ public/resources/script.js | 134 +++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 pkg/repository/repository.go create mode 100644 public/html/google-callback.html create mode 100644 public/html/telegram-callback.html create mode 100644 public/resources/script.js diff --git a/pkg/repository/repository.go b/pkg/repository/repository.go new file mode 100644 index 0000000..a94f114 --- /dev/null +++ b/pkg/repository/repository.go @@ -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), + } +} diff --git a/public/html/google-callback.html b/public/html/google-callback.html new file mode 100644 index 0000000..116f27b --- /dev/null +++ b/public/html/google-callback.html @@ -0,0 +1,33 @@ + + + + + + Login + + +< \ No newline at end of file diff --git a/public/html/telegram-callback.html b/public/html/telegram-callback.html new file mode 100644 index 0000000..b75baed --- /dev/null +++ b/public/html/telegram-callback.html @@ -0,0 +1,33 @@ + + + + + + Login + + +< \ No newline at end of file diff --git a/public/resources/script.js b/public/resources/script.js new file mode 100644 index 0000000..325aa4a --- /dev/null +++ b/public/resources/script.js @@ -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; +} \ No newline at end of file