-
Notifications
You must be signed in to change notification settings - Fork 10
/
router.js
101 lines (92 loc) · 3.09 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
console.log('router ==============')
import Vue from 'vue'
import Router from 'vue-router'
// import IndexPage from '~/pages/index.vue'
import initStore from '~/store'
import plugins from 'hubpress-core-plugins'
import { initializeStores } from '~/hubpress-plugins/services'
import { applicationPlugin } from '~/hubpress-plugins/application'
import { authenticationPlugin, LoginComponent } from '~/hubpress-plugins/authentication'
import { dashboardPlugin } from '~/hubpress-plugins/dashboard'
import { hubpressPlugin } from '~/hubpress-plugins/hubpress-blog'
import { githubPlugin } from '~/hubpress-plugins/hubpress-github'
import { templatePlugin } from '~/hubpress-plugins/hubpress-template'
import { sessionStoragePlugin } from '~/hubpress-plugins/hubpress-session-storage'
import { asciidocPlugin } from '~/hubpress-plugins/hubpress-asciidoc'
import { pouchDbPlugin } from '~/hubpress-plugins/hubpress-pouchdb'
import { lokijsPlugin } from '~/hubpress-plugins/hubpress-lokijs'
import { rssPlugin } from '~/hubpress-plugins/hubpress-rss'
console.log('Router')
Vue.use(Router)
plugins.register(
applicationPlugin,
authenticationPlugin,
dashboardPlugin,
hubpressPlugin,
githubPlugin,
templatePlugin,
sessionStoragePlugin,
asciidocPlugin,
// pouchDbPlugin,
lokijsPlugin,
rssPlugin,
)
console.log('Registered plugins', plugins.list())
export function createRouter(info) {
const opts = {
currentState: {
routes: []
},
}
const updatedOpts = plugins.fireSync('application:routes', opts);
const routerOptions = {
base: '/hubpress/',
mode: 'hash',
routes: [
// {
// path: '/',
// component: IndexPage
// },
{
path: '/login',
component: LoginComponent,
},
{
path: '/',
component: { template: '<router-view></router-view>' },
redirect: '/content',
meta: {
auth: true,
},
children: updatedOpts.nextState.routes
.map(route => {
console.log(route)
return route.entries
})
.reduce((memo, entries) => memo.concat(entries), [])
},
]
}
const router = new Router(routerOptions)
router.routes = updatedOpts.nextState.routes
router.beforeEach((to, from, next) => {
const store = initStore()
if (to.matched.some(record => record.meta.auth)) {
// this route requires auth, check if logged in
// if not, redirect to login page.
if (!store.state.authentication.isAuthenticated) {
next({
path: '/login',
query: {
redirect: to.fullPath
},
})
} else {
next()
}
} else {
next() // make sure to always call next()!
}
})
return router
}