genadmin/src/router.ts
2022-01-10 10:40:05 +00:00

161 lines
3.2 KiB
TypeScript

// eslint-disable-next-line no-unused-vars
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// import { defineAsyncComponent } from 'vue'
import generatedRoutes from 'virtual:generated-pages'
import useState from '~/hooks/useState'
import {has_config} from '~/hooks/config'
import {authExpired} from '~/hooks/utilsAuth'
// // const Login = defineAsyncComponent(() => import('./views/Login.vue'))
import Home from '~/views/Home.vue'
import Config from '~/views/Config.vue'
import Tracking from '~/views/Tracking.vue'
import Users from '~/views/Users.vue'
//import About from '~/pages/About.md'
import Register from '~/views/Register.vue'
import Login from '~/views/Login.vue'
import Logout from '~/views/Logout.vue'
// https://medium.com/swlh/adding-meta-fields-and-transitions-to-vue-router-routes-f5cb78a7ed97
const check_config = async(to: any, _from: any, next: any) => {
if (await has_config()) {
next()
} else {
next('/noconfig')
}
}
const check_auth = async(to: any, _from: any, next: any) => {
if (await has_config()) {
if (authExpired()) {
useState().sourceRoute.value = to.path
next('/login')
} else {
useState().sourceRoute.value = ""
next()
}
} else {
next('/login')
}
}
const routes: RouteRecordRaw[] = [
{
path: '/config',
name: 'Config',
component: Config,
meta: {
layout: 'AppLayout',
ctx: 'config',
withauth: true,
},
beforeEnter: check_auth,
},
{
path: '/tracking',
name: 'Tracking',
component: Tracking,
meta: {
requireAuth: true,
layout: 'AppLayout',
withauth: true,
},
beforeEnter: check_auth,
},
{
path: '/users',
name: 'Users',
component: Users,
meta: {
requireAuth: true,
layout: 'AppLayout',
withauth: true,
},
beforeEnter: check_auth,
},
{
path: '/',
name: 'Home',
component: Home,
meta: {
ctx: 'home',
withauth: true,
},
beforeEnter: check_auth,
},
{
path: '/login',
name: 'Login',
component: Login,
meta: {
requireAuth: false,
//layout: 'empty',
layout: 'AppLayout',
use_logo: true,
ctx: 'login',
withauth: false,
},
beforeEnter: check_config,
},
{
path: '/register/:id',
name: 'Register',
component: Register,
meta: {
requireAuth: false,
//layout: 'empty',
layout: 'AppLayout',
use_logo: true,
ctx: 'register',
withauth: false,
},
beforeEnter: check_config,
},
{
path: '/recoveryuser/:id',
name: 'Recovery',
component: Register,
meta: {
requireAuth: false,
//layout: 'empty',
layout: 'AppLayout',
use_logo: true,
ctx: 'register',
withauth: false,
},
beforeEnter: check_config,
},
{
path: '/logout',
name: 'Logout',
component: Logout,
meta: {
layout: 'AppLayout',
use_logo: true,
requireAuth: false,
ctx: 'logout',
withauth: false,
}
},
{
path: '/noconfig',
name: 'NoConfig',
component: () => import('./views/404.vue'),
meta: {
}
},
{
path: '/:catchAll(.*)',
name: '404',
meta: { layout: 'Page404' },
component: () => import('./views/404.vue'),
},
...generatedRoutes,
]
const router: any = createRouter({
history: createWebHistory(),
routes,
})
export default router