From 5ed5bbf6fef0a6da9f49100cd65b00052ea06fe4 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 20 Apr 2021 19:56:28 -0500 Subject: [PATCH] Added side nav and admin routes --- src/app/App.vue | 12 +++-- src/app/components/SideNav.vue | 71 ++++++++++++++++++++++++++ src/app/routes/index.ts | 38 ++++++++++++-- src/app/store/getters.ts | 20 +++++++- src/app/store/mutations.ts | 2 +- src/app/views/Dashboard.vue | 16 ++++++ src/app/views/admin/Admin.vue | 3 ++ src/app/views/admin/AdminDashboard.vue | 3 ++ 8 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 src/app/components/SideNav.vue create mode 100644 src/app/views/Dashboard.vue create mode 100644 src/app/views/admin/Admin.vue create mode 100644 src/app/views/admin/AdminDashboard.vue diff --git a/src/app/App.vue b/src/app/App.vue index dba0e1b..1f40cfc 100644 --- a/src/app/App.vue +++ b/src/app/App.vue @@ -1,19 +1,23 @@ + + diff --git a/src/app/routes/index.ts b/src/app/routes/index.ts index b2f27ce..d51e82e 100644 --- a/src/app/routes/index.ts +++ b/src/app/routes/index.ts @@ -25,6 +25,17 @@ function requiresAuth(to: RouteLocationNormalized, from: RouteLocationNormalized next(); } +/** + * Check if the user is an authenticated admin, redirect otherwise + */ +function requiresAdmin(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) { + if (store.getters.isAuthenticated && !store.getters.isAdmin) { + next({ name: "Home" }); + return; + } + next(); +} + /** * Fetch request providing authentication and logout upon unauthorized requests */ @@ -55,11 +66,13 @@ const routes: RouteRecordRaw[] = [ { path: "/", name: "Home", - component: { - beforeRouteEnter(to, from, next) { - next({ name: "Search" }); - } - } + redirect: to => ({ name: "Dashboard" }) + }, + { + path: "/dashboard", + name: "Dashboard", + component: () => import("../views/Dashboard.vue"), + beforeEnter: requiresAuth }, { path: "/search", @@ -75,6 +88,21 @@ const routes: RouteRecordRaw[] = [ } ] }, + { + path: "/admin", + name: "Admin", + redirect: to => ({ name: "AdminDashboard" }), + component: () => import("../views/admin/Admin.vue"), + beforeEnter: requiresAdmin, + children: [ + { + path: "dashboard", + name: "AdminDashboard", + component: () => import("../views/admin/AdminDashboard.vue"), + props: true + } + ] + }, { path: "/login", name: "Login", diff --git a/src/app/store/getters.ts b/src/app/store/getters.ts index 10dc9ce..0751bfc 100644 --- a/src/app/store/getters.ts +++ b/src/app/store/getters.ts @@ -2,9 +2,11 @@ import { GetterTree } from "vuex"; import { IState } from "./state"; export type GettersTypes = { + isAdmin (state: IState): boolean, isAuthenticated(state: IState): boolean, - storedToken(): string | null, - token(state: IState): string | null + storedToken () : string | null, + token (state: IState): string | null, + userName (state: IState): string | null } export const getters: GettersTypes & GetterTree = { @@ -15,6 +17,13 @@ export const getters: GettersTypes & GetterTree = { return state.user != null; }, + /** + * Determine if the user is an admin + */ + isAdmin(state: IState) { + return state.user?.isAdmin ?? false; + }, + /** * Retrieve the stored token */ @@ -27,5 +36,12 @@ export const getters: GettersTypes & GetterTree = { */ token(state: IState) { return state.user?.token ?? null; + }, + + /** + * Get the user's name (assumes authenticated) + */ + userName(state: IState) { + return state.user?.name.split(" ")[0] ?? null; } }; diff --git a/src/app/store/mutations.ts b/src/app/store/mutations.ts index cd91026..ed4a5cc 100644 --- a/src/app/store/mutations.ts +++ b/src/app/store/mutations.ts @@ -65,7 +65,7 @@ export const mutations: MutationsTypes & MutationTree = { state.user = { id : user.id, name : user.name, - isAdmin: false, + isAdmin: user.isAdmin, token : token } } catch(e) { diff --git a/src/app/views/Dashboard.vue b/src/app/views/Dashboard.vue new file mode 100644 index 0000000..11cb409 --- /dev/null +++ b/src/app/views/Dashboard.vue @@ -0,0 +1,16 @@ + + + diff --git a/src/app/views/admin/Admin.vue b/src/app/views/admin/Admin.vue new file mode 100644 index 0000000..233d571 --- /dev/null +++ b/src/app/views/admin/Admin.vue @@ -0,0 +1,3 @@ + diff --git a/src/app/views/admin/AdminDashboard.vue b/src/app/views/admin/AdminDashboard.vue new file mode 100644 index 0000000..53236e4 --- /dev/null +++ b/src/app/views/admin/AdminDashboard.vue @@ -0,0 +1,3 @@ +