summaryrefslogtreecommitdiffstats
path: root/webapp/src/router.js
blob: 470ffecf1214f925f2b6bc12bb518d46f4d30aea (plain) (blame)
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
import Vue from 'vue'
import Router from 'vue-router'
import DashboardPage from '@/components/DashboardPage'
import dashboardCategories from '@/config/dashboard'
import StartPage from '@/components/StartPage'

function setChildren (routes, parentName) {
  routes.forEach(route => {
    if (route.name && parentName) route.name = parentName + '.' + route.name
    else if (!parentName) route.name = route.component.name
    if (route.component && route.component.routes) {
      route.children = setChildren(route.component.routes(), route.name)
    }
  })
  return routes
}

Vue.use(Router)

var router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'start',
      component: StartPage,
      children: StartPage.routes()
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      redirect: '/dashboard/groups',
      component: DashboardPage,
      children: setChildren(DashboardPage.routes().concat(...dashboardCategories.map(c => c.modules)))
    },
    {
      path: '*',
      redirect: '/dashboard/groups'
    }
  ]
})

const registerRouterGuards = function (store) {
  router.beforeEach((to, from, next) => {
    const loggedIn = document.cookie.indexOf('jwt_hp') >= 0
    if (to.name === 'login') {
      if (loggedIn) next({ name: 'dashboard' })
      else next()
    } else if (to.name === 'start') {
      if (loggedIn) next({ name: 'dashboard' })
      else next({ name: 'login' })
    } else if (to.name === 'setup') {
      if (loggedIn) next({ name: 'dashboard' })
      else next()
    } else if (!loggedIn) {
      store.commit('setLoginRedirect', to.fullPath)
      next({ name: 'login' })
    } else {
      next()
    }
  })
}

export { router, registerRouterGuards }