Parcourir la source

connect to logout api

huaisianhuang il y a 4 ans
Parent
commit
f0fa7d8005

+ 1 - 0
html/script_eng.js

@@ -28,6 +28,7 @@ function checkRoute() {
     $('.userName').html(userName);
   }).catch(err => {
     console.log(err);
+    window.location.replace("login.html");
   });
 }
 

+ 1 - 0
html/script_long.js

@@ -31,6 +31,7 @@ function checkRoute() {
     $('.userName').html(userName);
   }).catch(err => {
     console.log(err);
+    window.location.replace("login.html");
   });
 }
 

+ 8 - 3
html/script_profiles.js

@@ -14,6 +14,7 @@ let userBasics = JSON.parse(localStorage.getItem('user_profile')) || [];
 
 function checkLocal() {
   if(userBasics.length == 0){ return };
+  console.log(userBasics);
   $('.userName').html(`<h2 class="user-name text-white mt-4 fw-bold">Hi ${userBasics.user_info.userName}</h2>`);
 }
 
@@ -39,8 +40,8 @@ checkLan();
 function renderView() {
     let token = getCookie('jwt_token');
     if(!token) {
-        return;
-     }
+        window.location.replace("login.html");
+    }
     axios({
         method: 'post',
         url: 'https://www.choozmo.com:8887/user_profile',
@@ -50,7 +51,10 @@ function renderView() {
          }
     }).then(res => {
         console.log(res.data);
-        const userInfo = res.data;
+        let userInfo = res.data;
+        if(!userInfo.user_info) {
+          window.location.replace("login.html");
+        }
         localStorage.setItem('user_profile', JSON.stringify(res.data));
           if(userInfo.user_info.left_sec < 20){
             let title = "剩餘秒數不足";
@@ -152,6 +156,7 @@ function renderView() {
             $('.card-profile').html(str);
     }).catch(err => {
         console.log(err);
+        window.location.replace("login.html");
     })
 }
 

+ 28 - 13
html/static/common.js

@@ -1,14 +1,29 @@
 $(".btn-logout").click(function() { 
-    axios({
-        method: 'get',
-        url: 'https://www.choozmo.com:8887/logout',
-        headers: { 
-            'accept': 'application/json',
-         }
-      }).then(res => {
-        console.log(res.data);
-        window.location.href = 'index.html';
-      }).catch(err => {
-        console.log(err);
-      });
-});
+  let token = getCookie('jwt_token');
+  if(!token) {
+    return
+  }
+  axios({
+    method: 'post',
+    url: 'https://www.choozmo.com:8887/logout_jwt',
+    headers: { 
+      'accept': 'application/json',
+      'Authorization': `Bearer ${token}`
+    },
+    data: ''
+    }).then(res => {
+      console.log(res.data);
+      if(res.data.msg == 'ok'){
+        document.cookie = 'jwt_token=null';
+      }
+      window.location.href = 'index.html';
+    }).catch(err => {
+      console.log(err);
+    });
+});
+
+function getCookie(name) {
+  const value = `; ${document.cookie}`;
+  const parts = value.split(`; ${name}=`);
+  if (parts.length === 2) return parts.pop().split(';').shift();
+}

+ 1 - 0
html/static/script_slides.js

@@ -27,6 +27,7 @@ function checkRoute() {
     $('.userName').html(userName);
   }).catch(err => {
     console.log(err);
+    window.location.replace("login.html");
   });
 }
 

+ 1 - 0
html/static/script_slides_eng.js

@@ -27,6 +27,7 @@ function checkRoute() {
     $('.userName').html(userName);
   }).catch(err => {
     console.log(err);
+    window.location.replace("login.html");
   });
 }
 

+ 1 - 0
html/static/script_util.js

@@ -30,6 +30,7 @@ function checkRoute() {
     $('.userName').html(userName);
   }).catch(err => {
     console.log(err);
+    window.location.replace("login.html");
   });
 }