jichoi / lms_front star
박민혁 박민혁 08-08
240808 박민혁 로그인 페이지 수정
@06fe962a568b0b477de23b88b0cc9928a178fa73
client/views/App.vue
--- client/views/App.vue
+++ client/views/App.vue
@@ -1,9 +1,9 @@
 <template>
    <div id="app">
       <!-- <Header></Header> -->
-               <router-view />
-                  <v-app >
-                  </v-app>
+      <router-view />
+      <v-app>
+      </v-app>
    </div>
    <!-- <div >
       
@@ -27,10 +27,12 @@
 import Menu from '../views/layout/Menu.vue';
 import Side from '../views/layout/Side.vue';
 // import Footer from '../views/layout/Footer.vue';
+import store from './pages/AppStore'
 
 const App = {
    data: () => {
       return {
+         store: store,
       }
    },
    methods: {
@@ -46,7 +48,7 @@
       Header: Header,
       Menu: Menu,
       // Footer:Footer,
-      Side:Side,
+      Side: Side,
    },
    mounted: () => {
       console.log('Vue mounted');
@@ -56,6 +58,4 @@
 export default App;
 </script>
 
-<style scoped>
-
-</style>
+<style scoped></style>
client/views/Login.vue
--- client/views/Login.vue
+++ client/views/Login.vue
@@ -153,6 +153,7 @@
 import SvgIcon from '@jamescoyle/vue-icon';
 import { mdiMagnify, mdiWindowClose } from '@mdi/js';
 import axios from "axios";
+import store from './pages/AppStore';
 
 export default {
    data() {
@@ -167,9 +168,11 @@
    },
    methods: {
       submitForm() {
-         // 여기에 로그인 로직을 추가하세요.
          console.log('Username:', this.username);
          console.log('Password:', this.password);
+         
+         const vm = this;
+
          axios({
                 url: "/auth/login.json",
                 method: "post",
@@ -178,15 +181,41 @@
                   password: this.password,
                 },
             }).then(function (response) {
-               alert("성공!")
+               const token = response.headers.get('Authorization');
+               store.dispatch('login', token);
+               vm.branchPage();
             }).catch(function (error) {
                 console.log(error);
             });
 
       },
-      goToPage(page) {
-         this.$router.push({ name: page });
-      },
+      branchPage() {
+         const vm = this;
+         const token = localStorage.getItem('token');
+         axios.post('/auth/validateToken.json', {}, {
+                headers: {
+                    Authorization: token
+                }
+            }).then(response => {
+                if (response.data.status === 'success') {
+                    const userInfo = response.data.userInfo;
+                    store.commit('setToken', token);
+                    store.commit('setUser', userInfo.usid);
+                    store.commit('setAuthcd', userInfo.author[0].authorCode);
+                    const roles = userInfo.author.map(role => role.authorCode);
+
+                    // 학생은 Main_t로 접근할 수 없음
+                    if (roles.includes("STUDENT")) {
+                     vm.goToPage('Dashboard');
+                    }
+                    // 선생님은 Main으로 접근할 수 없음
+                    else if (roles.includes("TEACHER")) {
+                     vm.goToPage('Board');
+                    } 
+                } 
+            }).catch(error => {
+                console.error(error);
+            });   },
       closeModal() {
          this.showModal = false;
       },
@@ -197,6 +226,10 @@
          this.searchOpen = false;
 
       },
+      goToPage(page) {
+      this.$router.push({ name: page });
+    },
+
    },
    components: {
       SvgIcon
 
client/views/pages/AppStore.js (added)
+++ client/views/pages/AppStore.js
@@ -0,0 +1,46 @@
+import { createStore } from 'vuex';
+
+export default createStore({
+    state: {
+        token: null,
+        userId: null,
+        authcd: null,
+    },
+    getters: {
+        isLoggedIn(state) {
+            return !!state.token; 
+        },
+        getUserInfo(state) {
+            return {
+                userId: state.userId,
+                authcd: state.authcd,
+            }; 
+        }
+    },
+    mutations: {
+        setToken(state, token) {
+            state.token = token;
+        },
+        clearToken(state) {
+            state.token = null;
+            state.userId = null; 
+            state.authcd = null; 
+        },
+        setUser(state, userId) {
+            state.userId = userId; 
+        },
+        setAuthcd(state, authcd) {
+            state.authcd = authcd; 
+        }
+    },
+    actions: {
+        login({ commit }, token) {
+            commit('setToken', token); 
+            localStorage.setItem('token', token);
+        },
+        logout({ commit }) {
+            commit('clearToken');
+            localStorage.removeItem('token');
+        },
+    }
+});
package-lock.json
--- package-lock.json
+++ package-lock.json
@@ -25,6 +25,7 @@
         "vue-router": "4.1.5",
         "vue-style-loader": "4.1.3",
         "vue3-sfc-loader": "^0.8.4",
+        "vuex": "^4.1.0",
         "webpack": "5.74.0",
         "webpack-cli": "4.10.0"
       }
@@ -3756,6 +3757,17 @@
       "integrity": "sha512-eziaIrk/N9f9OCpyFEkR6vMsZUHcF5mQslXjffwcb5Iq6EuU74QrlpBeJqA04MvAGT7f5O8la2v9k3NtQnJb3Q==",
       "license": "MIT"
     },
+    "node_modules/vuex": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/vuex/-/vuex-4.1.0.tgz",
+      "integrity": "sha512-hmV6UerDrPcgbSy9ORAtNXDr9M4wlNP4pEFKye4ujJF8oqgFFuxDCdOLS3eNoRTtq5O3hoBDh9Doj1bQMYHRbQ==",
+      "dependencies": {
+        "@vue/devtools-api": "^6.0.0-beta.11"
+      },
+      "peerDependencies": {
+        "vue": "^3.2.0"
+      }
+    },
     "node_modules/watchpack": {
       "version": "2.4.1",
       "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.4.1.tgz",
package.json
--- package.json
+++ package.json
@@ -20,6 +20,7 @@
     "vue-router": "4.1.5",
     "vue-style-loader": "4.1.3",
     "vue3-sfc-loader": "^0.8.4",
+    "vuex": "^4.1.0",
     "webpack": "5.74.0",
     "webpack-cli": "4.10.0"
   },
Add a comment
List