aboutsummaryrefslogtreecommitdiff
path: root/src/router/index.ts
blob: 496f5d061e06fb8c0483e4491b433f70bf2f0711 (plain)
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
import { createRouter, createWebHistory } from 'vue-router'

import Register from '../views/Register.vue'
import LogIn from '../views/LogIn.vue'
import HomeView from '../views/HomeView.vue'
import AllBrands from '../views/AllBrands.vue'
import AllCustomers from '../views/AllCustomers.vue'
import NewCustomer from '../views/NewCustomer.vue'
import AllItems from '../views/AllItems.vue'
import NewItem from '../views/NewItem.vue'
import NewInvoice from '../views/NewInvoice.vue'
import EditInvoice from '../views/EditInvoice.vue'
import ViewInvoice from '../views/ViewInvoice.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta: { isAuth: false }
    },
    {
      path: '/login',
      name: 'log in',
      component: LogIn,
      meta: { isAuth: false }
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: { isAuth: true }
    },
    {
      path: '/brand',
      name: 'brand',
      component: AllBrands,
      meta: { isAuth: true }
    },
    {
      path: '/customer',
      name: 'customer',
      component: AllCustomers,
      meta: { isAuth: true }
    },
    {
      path: '/customer/new',
      name: 'new-customer',
      component: NewCustomer,
      meta: { isAuth: true }
    },
    {
      path: '/item',
      name: 'item',
      component: AllItems,
      meta: { isAuth: true }
    },
    {
      path: '/item/new',
      name: 'new-item',
      component: NewItem,
      meta: { isAuth: true }
    },
    {
      path: '/invoice/new',
      name: 'new-invoice',
      component: NewInvoice,
      meta: { isAuth: true }
    },
    {
      path: '/invoice/edit/:id',
      name: 'edit-invoice',
      component: EditInvoice,
      meta: { isAuth: true }
    },
    {
      path: '/invoice/view/:id',
      name: 'view-invoice',
      component: ViewInvoice,
      meta: { isAuth: true }
    },
  ]
})

router.beforeEach((to, _, next) => {
  if (to.meta.isAuth && !localStorage.getItem('authToken')) {
    next('/login?redirected=true')
  } else {
    next()
  }
})

export default router