moonyeju 01-17
240117 김준식 로그인 기능 수정, 로그아웃 기능 추가
@35fe6c3bd6bbe0a2b8d1b5cddcbbd3ead723f578
client/views/layout/Header.vue
--- client/views/layout/Header.vue
+++ client/views/layout/Header.vue
@@ -14,9 +14,11 @@
                     <div class="user-img">
                         <svg-icon type="mdi" :path="mdiAccountCircle" color="#213f99"></svg-icon>
                     </div>
-                    <span class="user-name">000님</span>
+                    <span class="user-name" v-show="cookieExist">{{ userName }}님</span>
                 </div>
-                <button class="logout-btn">로그아웃</button>
+                <button class="logout-btn" @click="logout()" v-show="cookieExist">로그아웃</button>
+                <button class="logout-btn" @click="login()" v-show="!cookieExist">로그인</button>
+                
             </div>
         </div>
     </header>
@@ -25,6 +27,8 @@
 <script>
 import SvgIcon from '@jamescoyle/vue-icon'
 import { mdiEmail, mdiAccountCircle } from '@mdi/js'
+import axios from 'axios'
+
 export default {
     props: {
         className: String
@@ -32,15 +36,45 @@
     data() {
         return {
             mdiEmail: mdiEmail,
-            mdiAccountCircle: mdiAccountCircle
+            mdiAccountCircle: mdiAccountCircle,
+            cookieExist: null, // 쿠키 존재 여부
+            userName: $cookies.get("USER"), // 사용자 이름
         }
     },
     methods: {
+        // 로그아웃
+        logout: function () {
+            const vm = this;
+
+            axios({
+                url: '/logout.json',
+                method: 'post',
+                headers: { "Content-Type": "application/json; charset=UTF-8" },
+                data: {},
+            })
+                .then(function (response) {
+                    console.log("logout - response", response.data);
+                    $cookies.remove("USER");
+                    $cookies.remove("JSESSIONID");
+                    vm.cookieExist = false;
+                    alert("로그아웃하였습니다.");
+                    vm.$router.go();
+                    vm.$router.push({ path: '/', query: {} });
+                })
+                .catch(function (error) {
+                    console.log("logout - error", error);
+                    alert("로그아웃에 오류가 발생하였습니다.");
+                });
+
+        },
+
+        //로그인 버튼 클릭시 login.page로 이동
+        login: function () {
+            this.$router.push('/');
+        }
 
     },
-    watch: {
-
-    },
+    watch: {},
     computed: {
         logoStyle() {
             if (this.className === 'top') {
@@ -53,13 +87,23 @@
             } else {
                 return {}
             }
-        }
+        },
     },
     components: {
         'SvgIcon': SvgIcon
     },
     mounted() {
         console.log('Header mounted');
+
+        //쿠키 존재 유무 확인
+        if ($cookies.get("JSESSIONID") != null) {
+            this.cookieExist = true;
+        }
+        else {
+            // 없으면 로그인페이지로 이동
+            this.cookieExist = false;
+            this.$router.push('/');
+        }
     }
 }
 </script>
(파일 끝에 줄바꿈 문자 없음)
client/views/pages/login/Login.vue
--- client/views/pages/login/Login.vue
+++ client/views/pages/login/Login.vue
@@ -16,13 +16,15 @@
                         <router-link to="/register.page">회원가입</router-link>
                     </div>
                 </div>
-                <button class="blue-btn large-btn" v-on:click="login()">로그인</button>
+                <button class="blue-btn large-btn" @click="login()">로그인</button>
             </div>
         </div>
     </div>
 </template>
 
 <script>
+import axios from "axios";
+import vueCookie from "vue-cookies";
 export default {
     data() {
         return {
@@ -42,15 +44,15 @@
 
             // 아이디를 입력하지 않았을 경우
             // 공백만 입력하였을 경우
-            // 특수문자가 입려되었을 경우
-            if (vm.userData.user_id == null || vm.userLogin.user_id.replace(/\s/gi, '') == '' || special_pattern.test(vm.userLogin.user_id) == true) {
+            // 특수문자가 입력되었을 경우
+            if (vm.userLogin.user_id == null || vm.userLogin.user_id.replace(/\s/gi, '') == '' || special_pattern.test(vm.userLogin.user_id) == true) {
                 alert("아이디를 입력해주시기 바랍니다.");
                 return;
             }
 
             // 비밀번호를 입력하지 않았을 경우
             // 공백만 입력하였을 경우
-            // 특수문자가 입려되었을 경우
+            // 특수문자가 입력되었을 경우
             if (vm.userLogin.user_password == null || vm.userLogin.user_password.replace(/\s/gi, '') == '') {
                 alert("비밀번호를 입력해주시기 바랍니다.");
                 return;
@@ -60,15 +62,16 @@
                 url: '/login.json',
                 method: 'post',
                 headers: { "Content-Type": "application/json; charset=UTF-8" },
-                data: vm.userData,
+                data: vm.userLogin,
             })
                 .then(function (response) {
                     console.log("login - response", response.data);
                     if (response.data > 0) {
                         alert("로그인 성공하였습니다.");
-                        vm.$router.push({ path: '/', query: {} });
+                        vm.$router.go();
+                        vm.$router.push({ path: '/main.page', query: {} });
                     } else {
-                        alert("로그인 실패하였습니다. 아이디와 비밀번호를 다시 입력해주시기 바랍니다.");
+                        alert("로그인 실패하였습니다.\n\n아이디와 비밀번호를 다시 입력해주시기 바랍니다.");
                         return;
                     
                     }
@@ -79,11 +82,18 @@
                 });
         },
     },
-    watch: {},
+    watch: {
+
+        
+    },
     computed: {},
     components: {},
     mounted() {
         console.log('main mounted');
+        // 쿠키에 JSESSIONID 키 값이 존재한다면 메인페이지로 이동
+        if (vueCookie.get("JSESSIONID") != null) {
+            this.$router.push({ path: '/main.page', query: {} });
+        }
     },
 };
 </script>
package-lock.json
--- package-lock.json
+++ package-lock.json
@@ -1,5 +1,5 @@
 {
-  "name": "Data_Quality",
+  "name": "Data_Quality-1",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
@@ -23,6 +23,7 @@
         "pg": "8.8.0",
         "url-loader": "4.1.1",
         "vue": "3.2.40",
+        "vue-cookies": "^1.8.3",
         "vue-jstree": "^2.1.6",
         "vue-loader": "^17.0.0",
         "vue-router": "4.1.5",
@@ -4973,6 +4974,11 @@
         "@vue/shared": "3.2.40"
       }
     },
+    "node_modules/vue-cookies": {
+      "version": "1.8.3",
+      "resolved": "https://registry.npmjs.org/vue-cookies/-/vue-cookies-1.8.3.tgz",
+      "integrity": "sha512-VBRsyRMVdahBgFfh389TMHPmDdr4URDJNMk4FKSCfuNITs7+jitBDhwyL4RJd3WUsfOYNNjPAkfbehyH9AFuoA=="
+    },
     "node_modules/vue-jstree": {
       "version": "2.1.6",
       "resolved": "https://registry.npmjs.org/vue-jstree/-/vue-jstree-2.1.6.tgz",
@@ -9132,6 +9138,11 @@
         "@vue/shared": "3.2.40"
       }
     },
+    "vue-cookies": {
+      "version": "1.8.3",
+      "resolved": "https://registry.npmjs.org/vue-cookies/-/vue-cookies-1.8.3.tgz",
+      "integrity": "sha512-VBRsyRMVdahBgFfh389TMHPmDdr4URDJNMk4FKSCfuNITs7+jitBDhwyL4RJd3WUsfOYNNjPAkfbehyH9AFuoA=="
+    },
     "vue-jstree": {
       "version": "2.1.6",
       "resolved": "https://registry.npmjs.org/vue-jstree/-/vue-jstree-2.1.6.tgz",
package.json
--- package.json
+++ package.json
@@ -18,6 +18,7 @@
     "pg": "8.8.0",
     "url-loader": "4.1.1",
     "vue": "3.2.40",
+    "vue-cookies": "^1.8.3",
     "vue-jstree": "^2.1.6",
     "vue-loader": "^17.0.0",
     "vue-router": "4.1.5",
Add a comment
List