From 24d47644d45a3ab34c32035d6a08e88290ee4fed Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 29 Jan 2024 21:31:16 +0300 Subject: [PATCH] yandex auth, bug fixes --- index.js | 21 +++++++-- static/templates/index.html | 8 ++-- static/templates/user/settings.html | 39 ++++++++++++++++ static/templates/yandexauth.html | 58 ++++++++++++++++++++++- static/templates/yandexconnect.html | 72 +++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 10 deletions(-) create mode 100644 static/templates/yandexconnect.html diff --git a/index.js b/index.js index 719dcdd..d2de7e9 100644 --- a/index.js +++ b/index.js @@ -750,7 +750,7 @@ app.get( } ); -app.get("/project/yandexauth", async (req, res) => { +app.get("/yandexauth", async (req, res) => { const name = req.params.name; var templateData = { API_SERVER: process.env.API_SERVER, @@ -758,10 +758,21 @@ app.get("/project/yandexauth", async (req, res) => { Name: name, }; - const source = fs.readFileSync( - "static/templates/project/yandexauth.html", - "utf8" - ); + const source = fs.readFileSync("static/templates/yandexauth.html", "utf8"); + const template = handlebars.compile(source); + const resultT = template(templateData); + res.send(resultT); +}); + +app.get("/yandexconnect", async (req, res) => { + const name = req.params.name; + var templateData = { + API_SERVER: process.env.API_SERVER, + SITE_DOMAIN: process.env.SITE_DOMAIN, + Name: name, + }; + + const source = fs.readFileSync("static/templates/yandexconnect.html", "utf8"); const template = handlebars.compile(source); const resultT = template(templateData); res.send(resultT); diff --git a/static/templates/index.html b/static/templates/index.html index a5060c3..ac6032a 100644 --- a/static/templates/index.html +++ b/static/templates/index.html @@ -12,7 +12,7 @@ - + @@ -114,8 +114,8 @@ data-size="large" data-width="328" data-logo_alignment="left"> - -
--> + --> +
Забыли пароль? diff --git a/static/templates/user/settings.html b/static/templates/user/settings.html index 671ad23..8a82ed8 100644 --- a/static/templates/user/settings.html +++ b/static/templates/user/settings.html @@ -16,6 +16,30 @@ import { minidenticonSvg } from 'https://cdn.jsdelivr.net/npm/minidenticons@4.2.0/minidenticons.min.js' + + @@ -188,6 +212,21 @@ + +
+
+

Авторизация через Яндекс

+ {{#if User.yandexid}} +

Аккаунт Яндекса успешно привязан

+ {{else}} +

Если хотите входить в аккаунт через Яндекс, вам необходимо привязать его ниже

+ {{/if}} +
+
+

Нажмите на кнопочку справа.

+
+
+
diff --git a/static/templates/yandexauth.html b/static/templates/yandexauth.html index 30de51a..5bfbca2 100644 --- a/static/templates/yandexauth.html +++ b/static/templates/yandexauth.html @@ -5,10 +5,16 @@ + @@ -21,7 +27,57 @@ "kek": true, }); }; + + function getCookie(name) { + var cookies = document.cookie.split(";"); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + if (cookie.startsWith(name + "=")) { + return cookie.substring(name.length + 1); + } + } + return null; + } + + document.addEventListener('DOMContentLoaded', async function () { + const fragment = window.location.hash.substring(1); + + if (fragment.includes('access_token')) { + const accessToken = fragment.split('&')[0].split('=')[1]; + console.log(accessToken); + + const response = await fetch('{{API_SERVER}}/yandexauth?token=' + accessToken, { + method: "POST", + headers: { + Authorization: getCookie("token"), + "Content-Type": "application/json" + } + }); + + if (response.status === 200) { + const data = await response.json(); + const token = data.token; + + document.cookie = "token=" + token; + + window.location.href = "/projects"; + } else if (response.status === 401) { + alert("Сначала нужно привязать аккаунт Яндекса в настройках"); + location.href= '/'; + } else if (response.status === 403) { + alert("Неверный токен"); + location.href= '/'; + } else { + alert("Ошибка авторизации"); + location.href= '/'; + } + } + }); + + +

Пожалуйста, подождите...

+ diff --git a/static/templates/yandexconnect.html b/static/templates/yandexconnect.html new file mode 100644 index 0000000..88bbbca --- /dev/null +++ b/static/templates/yandexconnect.html @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + +

Пожалуйста, подождите...

+ + + +