From deb00436f5350c14eb63bff1b70bdaa5d71a3487 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Fri, 27 Jan 2023 17:17:47 +0300 Subject: [PATCH] perf(web): add `validate` function to js --- web/public/index.html | 1 + web/public/js/redirector.js | 1 + web/public/js/token.js | 9 +++++---- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 web/public/js/redirector.js diff --git a/web/public/index.html b/web/public/index.html index ae869e2..3668a6c 100644 --- a/web/public/index.html +++ b/web/public/index.html @@ -26,6 +26,7 @@ +

Welcome to Tanabata!

diff --git a/web/public/js/redirector.js b/web/public/js/redirector.js new file mode 100644 index 0000000..5e9510a --- /dev/null +++ b/web/public/js/redirector.js @@ -0,0 +1 @@ +$(window).on("load", validate(() => {}, () => $(location).attr("href", "/auth"))); diff --git a/web/public/js/token.js b/web/public/js/token.js index 5c5e860..7c8acd0 100644 --- a/web/public/js/token.js +++ b/web/public/js/token.js @@ -1,4 +1,4 @@ -$(window).on("load", function () { +function validate(onsuccess, onfailure) { let authorized = true; if ($.cookie("token") == null) { authorized = false; @@ -17,7 +17,8 @@ $(window).on("load", function () { } }); } - if (!authorized) { - $(location).attr("href", "/auth"); + if (authorized) { + return onsuccess; } -}); + return onfailure; +}