index.js 1.09 KB
Newer Older
牛文敏's avatar
init    
牛文敏 committed
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
import { createRouter, createWebHistory } from 'vue-router'
import { useStore } from '../stores'
import HomeView from '../views/HomeView.vue'
import NetView from '../views/NetView.vue'
import AuthView from '../views/AuthView.vue'
import SettingsView from '../views/SettingsView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/net',
      name: 'net',
      component: NetView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/settings',
      name: 'settings',
      component: SettingsView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/auth',
      name: 'auth',
      component: AuthView
    },
    {
      path: '/:path(.*)',
      redirect: '/'
    }
  ]
})

router.beforeEach((to) => {
  const store = useStore()
  if (to.meta.requiresAuth && !store.logged) return '/auth'
  if (to.name === 'auth' && store.logged) return '/'
})

export default router