summaryrefslogtreecommitdiffstats
path: root/webapp/src/router.js
blob: 2f85448ccd1aefbf22e053b46c6ad8aa895110ba (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
import Vue from 'vue'
import Router from 'vue-router'
import LoginPage from '@/components/LoginPage'
import DashboardPage from '@/components/DashboardPage'
import dashboardModules from '@/config/dashboard'

Vue.use(Router)

function setChildren (routes) {
  routes.forEach(route => {
    if (route.component && route.component.routes) {
      route.children = setChildren(route.component.routes())
    }
  })
  return routes
}

var router = new Router({
  routes: [
    {
      path: '/login',
      name: 'LoginPage',
      component: LoginPage
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: DashboardPage,
      children: setChildren(dashboardModules.concat(DashboardPage.routes()))
    },
    {
      path: '*',
      redirect: '/dashboard'
    }
  ]
})

router.beforeEach((to, from, next) => {
  var loggedIn = document.cookie.indexOf('jwt_hp') >= 0
  if (to.path === '/login') {
    if (loggedIn) next('/dashboard')
    else next()
  } else if (!loggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router