diff --git a/src/components/AppHeader.vue b/src/components/AppHeader.vue index 01e7f6e..b6b392c 100755 --- a/src/components/AppHeader.vue +++ b/src/components/AppHeader.vue @@ -13,6 +13,7 @@ Legal + Dashboard diff --git a/src/router/index.js b/src/router/index.js index 5aaad24..31e9946 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,6 +1,7 @@ import Vue from "vue"; import VueRouter from "vue-router"; import NotFound from "../components/NotFound.vue"; +import store from "../store"; Vue.use(VueRouter); @@ -24,6 +25,20 @@ const routes = [ component: () => import(/* webpackChunkName: "AppLegal" */ "../views/AppLegal.vue") }, + { + path: "/dashboard", + name: "Dashboard", + meta: { + requiresAuth: true + }, + component: () => + import(/* webpackChunkName: "Dashboard" */ "../views/UserDashboard") + }, + { + path: "/login", + name: "Login", + component: () => import(/* webpackChunkName: "Login" */ "../views/AppLogin") + }, { path: "/user/:username", name: "Users", @@ -71,4 +86,16 @@ const router = new VueRouter({ } }); +router.beforeEach((to, from, next) => { + if (to.matched.some(record => record.meta.requiresAuth)) { + if (store.state.auth) { + next(); + } else { + next({ name: "Login" }); + } + } else { + next(); + } +}); + export default router; diff --git a/src/store/index.js b/src/store/index.js index e95bf73..967f601 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -5,14 +5,30 @@ Vue.use(Vuex); const store = new Vuex.Store({ state: { - users: [] + users: [], + username: null, + auth: false }, mutations: { setUsers(state, users) { state.users = users; + }, + doLogin(state, username) { + state.auth = true; + state.username = username; + }, + doLogout(state) { + state.auth = false; + state.username = null; } }, actions: { + doLogin({ commit }, username) { + commit("doLogin", username); + }, + doLogout({ commit }) { + commit("doLogout"); + }, async setUsers({ commit }) { const users = window.localStorage.getItem("users"); if (users) { diff --git a/src/views/AppLogin.vue b/src/views/AppLogin.vue new file mode 100644 index 0000000..53c5081 --- /dev/null +++ b/src/views/AppLogin.vue @@ -0,0 +1,56 @@ + + + + + diff --git a/src/views/UserDashboard.vue b/src/views/UserDashboard.vue new file mode 100644 index 0000000..3522a16 --- /dev/null +++ b/src/views/UserDashboard.vue @@ -0,0 +1,25 @@ + + + + +