2021-08-14 16:19:33 +02:00
|
|
|
/**
|
|
|
|
* This is the router config, which defined the location for
|
|
|
|
* each page within the app, and how they should be loaded
|
|
|
|
* Note that the page paths are defined in @/utils/defaults.js
|
|
|
|
*/
|
|
|
|
|
2021-08-14 16:06:01 +02:00
|
|
|
// Import Vue.js and vue router
|
2019-09-01 14:38:13 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
import Router from 'vue-router';
|
2021-09-19 22:18:41 +02:00
|
|
|
import ProgressBar from 'rsup-progress';
|
2021-08-14 16:19:33 +02:00
|
|
|
|
2021-09-24 01:13:25 +02:00
|
|
|
// Import views, that are not lazy-loaded
|
2021-06-14 21:44:07 +02:00
|
|
|
import Home from '@/views/Home.vue';
|
|
|
|
import Login from '@/views/Login.vue';
|
2021-06-15 15:22:22 +02:00
|
|
|
import Workspace from '@/views/Workspace.vue';
|
2021-07-21 18:12:42 +02:00
|
|
|
import Minimal from '@/views/Minimal.vue';
|
2021-08-14 16:19:33 +02:00
|
|
|
|
2021-08-14 16:06:01 +02:00
|
|
|
// Import helper functions, config data and defaults
|
2021-08-21 23:29:21 +02:00
|
|
|
import { isAuthEnabled, isLoggedIn, isGuestAccessEnabled } from '@/utils/Auth';
|
2021-07-04 10:04:21 +02:00
|
|
|
import { config } from '@/utils/ConfigHelpers';
|
2021-08-14 16:19:33 +02:00
|
|
|
import { metaTagData, startingView, routePaths } from '@/utils/defaults';
|
2021-09-24 01:13:25 +02:00
|
|
|
import ErrorHandler from '@/utils/ErrorHandler';
|
2021-05-17 19:53:35 +02:00
|
|
|
|
2021-06-14 21:44:07 +02:00
|
|
|
Vue.use(Router);
|
2021-09-19 22:18:41 +02:00
|
|
|
const progress = new ProgressBar({ color: 'var(--progress-bar)' });
|
2021-06-13 10:40:40 +02:00
|
|
|
|
2021-08-18 22:23:39 +02:00
|
|
|
/* Returns true if user is already authenticated, or if auth is not enabled */
|
2021-06-13 10:40:40 +02:00
|
|
|
const isAuthenticated = () => {
|
2021-08-21 23:29:21 +02:00
|
|
|
const authEnabled = isAuthEnabled();
|
|
|
|
const userLoggedIn = isLoggedIn();
|
|
|
|
const guestEnabled = isGuestAccessEnabled();
|
|
|
|
return (!authEnabled || userLoggedIn || guestEnabled);
|
2021-06-13 10:40:40 +02:00
|
|
|
};
|
|
|
|
|
2021-08-14 16:06:01 +02:00
|
|
|
/* Get the users chosen starting view from app config, or return default */
|
2021-08-14 16:19:33 +02:00
|
|
|
const getStartingView = () => config.appConfig.startingView || startingView;
|
2021-08-14 16:06:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the component that should be rendered at the base path,
|
|
|
|
* Defaults to Home, but the user can change this to Workspace of Minimal
|
|
|
|
*/
|
|
|
|
const getStartingComponent = () => {
|
|
|
|
const usersPreference = getStartingView();
|
|
|
|
switch (usersPreference) {
|
|
|
|
case 'default': return Home;
|
|
|
|
case 'minimal': return Minimal;
|
|
|
|
case 'workspace': return Workspace;
|
|
|
|
default: return Home;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Returns the meta tags for each route */
|
|
|
|
const makeMetaTags = (defaultTitle) => ({
|
|
|
|
title: config.pageInfo.title || defaultTitle,
|
|
|
|
metaTags: metaTagData,
|
|
|
|
});
|
|
|
|
|
|
|
|
/* List of all routes, props, components and metadata */
|
2021-04-03 10:08:28 +02:00
|
|
|
const router = new Router({
|
2019-07-19 16:07:26 +02:00
|
|
|
routes: [
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // The default view can be customized by the user
|
2019-07-19 16:07:26 +02:00
|
|
|
path: '/',
|
2021-08-14 16:06:01 +02:00
|
|
|
name: `landing-page-${getStartingView()}`,
|
|
|
|
component: getStartingComponent(),
|
|
|
|
meta: makeMetaTags('Home Page'),
|
|
|
|
},
|
|
|
|
{ // Default home page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.home,
|
2019-07-19 16:07:26 +02:00
|
|
|
name: 'home',
|
2019-09-01 14:38:13 +02:00
|
|
|
component: Home,
|
2021-08-14 16:06:01 +02:00
|
|
|
meta: makeMetaTags('Home Page'),
|
2021-06-15 15:22:22 +02:00
|
|
|
},
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // Workspace view page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.workspace,
|
2021-06-15 15:22:22 +02:00
|
|
|
name: 'workspace',
|
|
|
|
component: Workspace,
|
2021-08-14 16:06:01 +02:00
|
|
|
meta: makeMetaTags('Workspace'),
|
2019-07-19 16:07:26 +02:00
|
|
|
},
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // Minimal view page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.minimal,
|
2021-07-21 18:12:42 +02:00
|
|
|
name: 'minimal',
|
|
|
|
component: Minimal,
|
2021-08-14 16:06:01 +02:00
|
|
|
meta: makeMetaTags('Start Page'),
|
2021-07-21 18:12:42 +02:00
|
|
|
},
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // The login page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.login,
|
2021-06-13 10:40:40 +02:00
|
|
|
name: 'login',
|
|
|
|
component: Login,
|
|
|
|
beforeEnter: (to, from, next) => {
|
2021-08-18 22:23:39 +02:00
|
|
|
// If the user already logged in + guest mode not enabled, then redirect home
|
2021-08-21 23:29:21 +02:00
|
|
|
if (isAuthenticated() && !isGuestAccessEnabled()) router.push({ path: '/' });
|
2021-06-13 10:40:40 +02:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
},
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // The about app page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.about,
|
2021-08-18 22:23:39 +02:00
|
|
|
name: 'about', // We lazy load the About page so as to not slow down the app
|
2021-09-24 01:13:25 +02:00
|
|
|
component: () => import('./views/About.vue'),
|
2021-08-14 16:06:01 +02:00
|
|
|
meta: makeMetaTags('About Dashy'),
|
2019-09-01 14:38:13 +02:00
|
|
|
},
|
2021-08-14 16:06:01 +02:00
|
|
|
{ // The export config page
|
2021-08-14 16:19:33 +02:00
|
|
|
path: routePaths.download,
|
2021-06-21 14:07:49 +02:00
|
|
|
name: 'download',
|
2021-09-24 01:13:25 +02:00
|
|
|
component: () => import('./views/DownloadConfig.vue'),
|
2021-08-14 16:06:01 +02:00
|
|
|
meta: makeMetaTags('Download Config'),
|
2021-06-21 14:07:49 +02:00
|
|
|
},
|
2021-09-24 01:13:25 +02:00
|
|
|
{ // Page not found, any non-defined routes will land here
|
|
|
|
path: routePaths.notFound,
|
|
|
|
name: '404',
|
|
|
|
component: () => import('./views/404.vue'),
|
|
|
|
meta: makeMetaTags('404 Not Found'),
|
|
|
|
beforeEnter: (to, from, next) => {
|
|
|
|
if (to.redirectedFrom) { // Log error, if redirected here from another route
|
|
|
|
ErrorHandler(`Route not found: '${to.redirectedFrom}'`);
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ // Redirect any not-found routed to the 404 view
|
|
|
|
path: '*',
|
|
|
|
redirect: '/404',
|
|
|
|
},
|
2019-09-01 14:38:13 +02:00
|
|
|
],
|
|
|
|
});
|
2021-04-03 10:08:28 +02:00
|
|
|
|
2021-08-14 16:06:01 +02:00
|
|
|
/**
|
2021-08-18 22:23:39 +02:00
|
|
|
* Before loading a route, check if the user has authentication enabled
|
|
|
|
* if so, then ensure that they are correctly logged in as a valid user
|
|
|
|
* If not logged in, prevent all access and redirect them to login page
|
2021-08-14 16:06:01 +02:00
|
|
|
* */
|
2021-06-13 10:40:40 +02:00
|
|
|
router.beforeEach((to, from, next) => {
|
2021-09-19 22:18:41 +02:00
|
|
|
progress.start();
|
2021-06-13 10:40:40 +02:00
|
|
|
if (to.name !== 'login' && !isAuthenticated()) next({ name: 'login' });
|
|
|
|
else next();
|
|
|
|
});
|
|
|
|
|
2021-08-14 16:06:01 +02:00
|
|
|
/* If title is missing, then apply default page title */
|
2021-04-03 10:08:28 +02:00
|
|
|
router.afterEach((to) => {
|
2021-09-19 22:18:41 +02:00
|
|
|
progress.end();
|
2021-04-03 10:08:28 +02:00
|
|
|
Vue.nextTick(() => {
|
2021-08-14 16:06:01 +02:00
|
|
|
document.title = to.meta.title || 'Dashy';
|
2021-04-03 10:08:28 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-18 22:23:39 +02:00
|
|
|
// All done - export the now configured router
|
2021-04-03 10:08:28 +02:00
|
|
|
export default router;
|