From dbf57183de820f702071021b2efe26b25612b220 Mon Sep 17 00:00:00 2001 From: strokh24 <Rokas.Stankunas@Student.Reutlingen-University.DE> Date: Mon, 14 Oct 2024 23:31:41 +0200 Subject: [PATCH] Moved authorization check into a separate file --- public/index.html | 1 + public/js/authorization.js | 11 +++++++++++ public/js/index.js | 10 ---------- 3 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 public/js/authorization.js diff --git a/public/index.html b/public/index.html index ca170f1..97a91fc 100644 --- a/public/index.html +++ b/public/index.html @@ -6,6 +6,7 @@ <title>ToDo</title> <link rel="stylesheet" href="./css/index.css" /> <script src="./js/index.js" defer></script> + <script src="./js/authorization.js" defer></script> </head> <body> <div class="container"> diff --git a/public/js/authorization.js b/public/js/authorization.js new file mode 100644 index 0000000..61df3f5 --- /dev/null +++ b/public/js/authorization.js @@ -0,0 +1,11 @@ +document.addEventListener('DOMContentLoaded', async () => { + // Event for checking if the user is logged in + try { + const res = await fetch('/api/users/loggedin', { method: 'POST' }); + if (!res.ok) { + window.location.href = 'login.html'; + } + } catch (err) { + console.error('Error while checking if a user is logged in:', err); + } +}); \ No newline at end of file diff --git a/public/js/index.js b/public/js/index.js index a253202..5d80645 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -1,14 +1,4 @@ document.addEventListener('DOMContentLoaded', async () => { - // Event for checking if the user is logged in - try { - const res = await fetch('/api/users/loggedin', { method: 'POST' }); - if (!res.ok) { - window.location.href = 'login.html'; - } - } catch (err) { - console.error('Error while checking if a user is logged in:', err); - } - const logoutBtn = document.getElementById('logout-btn'); // Event for the logout button -- GitLab