diff --git a/src/components/common/LoginComponent.vue b/src/components/common/LoginComponent.vue index f784058..ffae988 100644 --- a/src/components/common/LoginComponent.vue +++ b/src/components/common/LoginComponent.vue @@ -93,11 +93,14 @@ export default { // localStorage를 사용하면, 브라우저에 key-value 값을 Storage에 저장할 수 있습니다 localStorage.setItem(process.env.VUE_APP_LOCALSTORAGE_NAME, result) alert('로그인 성공...') - // this.$router.push('/') + console.log('로그인 성공...') + console.log(this.$router) + this.$router.push(this.$reouter.path) } else { console.log(result) localStorage.removeItem(process.env.VUE_APP_LOCALSTORAGE_NAME) alert('로그인 실패...') + this.$router.back() } }, onReset() { diff --git a/src/router/index.js b/src/router/index.js index 4ffb39d..9b5b56d 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -21,19 +21,20 @@ const routes = [ }, { path: '/login', - name: 'LoginComponent', + name: 'login', component: () => import( - /* webpackChunkName: "api", webpackPrefetch:true */ '@/views/loginView.vue' + /* webpackChunkName: "auth", webpackPrefetch:true */ '@/views/loginView.vue' ) }, { path: '/todo', - name: 'TodoListView', + name: 'todo', component: () => import( /* webpackChunkName: "api", webpackPrefetch:true */ '@/views/todo/listView.vue' - ) + ), + meta: { requiresAuth: true } } ] @@ -43,4 +44,27 @@ const router = new VueRouter({ routes }) +// 참고 : +// https://vueschool.io/lessons/how-to-configure-an-authentication-middleware-route-guard-with-vue-router +// https://joshua1988.github.io/web-development/vuejs/vue-router-navigation-guards/ +// 로그인 처리 과정용 +// to : 이동할 url 정보가 담긴 라우터 객체 +// from : 현재 url 정보가 담긴 라우터 객체 +// next : to에서 지정한 url로 이동하기 위해 꼭 호출해야 하는 함수 +//여기에서 login은 path가 아니라 name에서 찾는다 +router.beforeEach((to, from, next) => { + //routes 설정에서 meta: { requiresAuth: true } 가 선언된 경우 + if (to.matched.some((record) => record.meta.requiresAuth)) { + if (!localStorage.getItem(process.env.VUE_APP_LOCALSTORAGE_NAME)) { + next({ name: 'login' }) + } else { + console.log(from.path + " => 인증됨 => '" + to.path + "'") + next() + } + } else { + console.log(from.path + " => 인증않됨 => '" + to.path + "'") + next() + } +}) + export default router diff --git a/src/views/loginView.vue b/src/views/loginView.vue index 3d8fa84..2c1fd9a 100644 --- a/src/views/loginView.vue +++ b/src/views/loginView.vue @@ -1,9 +1,7 @@