-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from denOldTimer/chapter-nine
Chapter nine
- Loading branch information
Showing
8 changed files
with
106 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
import router from './router' | ||
import store from './store' | ||
|
||
createApp(App).use(router).mount('#app') | ||
createApp(App).use(store).use(router).mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,51 @@ | ||
import { createRouter, createWebHistory } from 'vue-router' | ||
import Home from '../views/Home.vue' | ||
import UserProfile from '../views/UserProfile.vue' | ||
import Admin from '@/views/Admin' | ||
|
||
import { createRouter, createWebHistory } from "vue-router"; | ||
import store from "@/store"; | ||
import { users } from "@/assets/users"; | ||
import Home from "../views/Home.vue"; | ||
import UserProfile from "../views/UserProfile.vue"; | ||
import Admin from "@/views/Admin"; | ||
|
||
const routes = [ | ||
{ | ||
path: '/', | ||
name: 'Home', | ||
component: Home | ||
path: "/", | ||
name: "Home", | ||
component: Home, | ||
}, | ||
{ | ||
path: '/user/:userId', | ||
name: 'UserProfile', | ||
component: UserProfile | ||
path: "/user/:userId", | ||
name: "UserProfile", | ||
component: UserProfile, | ||
}, | ||
|
||
{ | ||
path: '/admin', | ||
name: 'Admin', | ||
path: "/admin", | ||
name: "Admin", | ||
component: Admin, | ||
meta: { | ||
requiresAdmin: true | ||
} | ||
} | ||
|
||
] | ||
requiresAdmin: true, | ||
}, | ||
}, | ||
]; | ||
|
||
const router = createRouter({ | ||
//history: createWebHashHistory(), remove the hashtag in the url | ||
history: createWebHistory(), | ||
routes | ||
routes, | ||
}); | ||
|
||
|
||
//router.beforeEach(async (to, from, next)) | ||
router.beforeEach(async(to, from, next) => { | ||
router.beforeEach(async (to, from, next) => { | ||
const user = store.state.User.user; | ||
|
||
if (!user) { | ||
//get user from api | ||
await store.dispatch("User/setUser", users[0]); | ||
} | ||
const isAdmin = true; | ||
const requiresAdmin = to.matched.some(record => record.meta.requiresAdmin); | ||
const requiresAdmin = to.matched.some((record) => record.meta.requiresAdmin); | ||
|
||
if (requiresAdmin && !isAdmin) next({ name: 'Home' }) | ||
if (requiresAdmin && !isAdmin) next({ name: "Home" }); | ||
else next(); | ||
}) | ||
}); | ||
|
||
export default router | ||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const UserModule = { | ||
namespaced: true, | ||
|
||
state: { | ||
user: null, | ||
}, | ||
|
||
// Mutations are functions that effect the state | ||
mutations: { | ||
SET_USER(state, user) { | ||
state.user = user; | ||
}, | ||
}, | ||
|
||
// Actions are functions that you call throughout your application that call mutations | ||
actions: { | ||
setUser({ commit }, user) { | ||
commit("SET_USER", user); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createStore } from "vuex"; | ||
import { UserModule } from "./User"; | ||
|
||
export default createStore({ | ||
state: {}, | ||
|
||
// Mutations are functions that effect the state | ||
mutations: {}, | ||
|
||
// Actions are functions that you call throughout your application that call mutations | ||
actions: {}, | ||
|
||
modules: { | ||
User: UserModule, | ||
}, | ||
}); |