dashy/src/router.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-09-01 14:38:13 +02:00
import Vue from 'vue';
import Router from 'vue-router';
2019-07-19 16:07:26 +02:00
import Home from '@/views/Home.vue';
import Login from '@/views/Login.vue';
import Workspace from '@/views/Workspace.vue';
import Minimal from '@/views/Minimal.vue';
2021-06-21 14:07:49 +02:00
import DownloadConfig from '@/views/DownloadConfig.vue';
import { isLoggedIn } from '@/utils/Auth';
import { config } from '@/utils/ConfigHelpers';
import { metaTagData } from '@/utils/defaults';
Vue.use(Router);
/**
* Checks if the current user is either authenticated,
* or if authentication is not enabled
* @returns true if user logged in, or user management not enabled
*/
const isAuthenticated = () => {
const users = config.appConfig.auth;
return (!users || users.length === 0 || isLoggedIn(users));
};
const router = new Router({
2019-07-19 16:07:26 +02:00
routes: [
{
path: '/',
name: 'home',
2019-09-01 14:38:13 +02:00
component: Home,
props: config,
meta: {
title: config.pageInfo.title || 'Home Page',
metaTags: metaTagData,
},
},
{
path: '/workspace',
name: 'workspace',
component: Workspace,
props: config,
meta: {
title: config.pageInfo.title || 'Dashy Workspace',
metaTags: metaTagData,
},
2019-07-19 16:07:26 +02:00
},
{
path: '/minimal',
name: 'minimal',
component: Minimal,
props: config,
meta: {
title: config.pageInfo.title || 'Dashy Start Page',
metaTags: metaTagData,
},
},
{
path: '/login',
name: 'login',
component: Login,
props: {
appConfig: config.appConfig,
},
beforeEnter: (to, from, next) => {
if (isAuthenticated()) router.push({ path: '/' });
next();
},
},
2019-07-19 16:07:26 +02:00
{
path: '/about',
name: 'about',
2019-09-01 14:38:13 +02:00
component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
},
2021-06-21 14:07:49 +02:00
{
path: '/download',
name: 'download',
component: DownloadConfig,
props: config,
2021-06-21 14:07:49 +02:00
meta: {
title: config.pageInfo.title || 'Download Dashy Config',
2021-06-21 14:07:49 +02:00
metaTags: metaTagData,
},
},
2019-09-01 14:38:13 +02:00
],
});
router.beforeEach((to, from, next) => {
if (to.name !== 'login' && !isAuthenticated()) next({ name: 'login' });
else next();
});
const defaultTitle = 'Dashy';
router.afterEach((to) => {
Vue.nextTick(() => {
document.title = to.meta.title || defaultTitle;
});
});
export default router;