浏览代码

added redirects to auth.html when a user is not logged in

Liontix 11 月之前
父节点
当前提交
bce51ffd27

+ 1 - 0
public/index.html

@@ -3,6 +3,7 @@
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
+    <script src="javascripts/index.js" async></script>
 </head>
 <body>
 

+ 12 - 0
public/javascripts/appointments.js

@@ -94,6 +94,11 @@ async function deleteAppointment(appointmentId) {
         method: 'DELETE',
         body: JSON.stringify({appointmentId: appointmentId})
     });
+    if (response.status === 401) {
+        window.location.replace("/html/auth.html");
+        return;
+    }
+
     if (response.ok) {
         await fetchAppointments();
     }
@@ -101,10 +106,17 @@ async function deleteAppointment(appointmentId) {
 
 async function fetchAppointments() {
     const response = await fetchWithToken("/api/users/appointments");
+    if (response.status === 401) {
+        window.location.replace("/html/auth.html");
+        return;
+    }
+
     const js = await response.json();
     if (!response.ok) {
         window.location.replace("/html/404.html");
     }
+
+
     renderAppointments(js);
 }
 

+ 1 - 1
public/javascripts/auth.js

@@ -38,7 +38,7 @@ async function sendAuthRequest(type) {
 
     if (response.status === 200) {
         localStorage.setItem("token", response_json["token"]);
-        window.location.replace("/");
+        window.location.replace("/html/appointments.html");
     } else {
         if (response_json["error"]) {
             errorMessageParagraph.innerText = response_json["error"];

+ 5 - 0
public/javascripts/index.js

@@ -0,0 +1,5 @@
+"use strict";
+
+window.onload = function() {
+    window.location.replace("/html/appointments.html");
+}

+ 0 - 1
public/stylesheets/appointments.css

@@ -6,7 +6,6 @@ body {
     display: flex;
     justify-content: center;
     align-items: center;
-    height: 100vh;
     box-sizing: border-box;
 }