bcgov/common-forms-toolkit

View on GitHub
app/frontend/src/router/index.js

Summary

Maintainability
A
1 hr
Test Coverage
C
76%
import NProgress from 'nprogress';
import Vue from 'vue';
import VueRouter from 'vue-router';

import comfort from './comfort';
import agriSeafoodOpScreening from './agriSeafoodOpScreening';
import farmOpScreening from './farmOpScreening';
import forestrySectorOpScreening from './forestrySectorOpScreening';
import minesOperatorScreening from './minesOperatorScreening';

Vue.use(VueRouter);

let isFirstTransition = true;

/**
 * Constructs and returns a Vue Router object
 * @param {string} [basePath='/'] the base server path
 * @returns {object} a Vue Router object
 */
export default function getRouter(basePath = '/') {
  const router = new VueRouter({
    base: basePath,
    mode: 'history',
    routes: [
      ...comfort,
      ...agriSeafoodOpScreening,
      ...farmOpScreening,
      ...forestrySectorOpScreening,
      ...minesOperatorScreening
    ]
  });

  router.beforeEach((to, _from, next) => {
    NProgress.start();
    if (to.matched.some(route => route.meta.requiresAuth)
      && router.app.$keycloak
      && router.app.$keycloak.ready
      && !router.app.$keycloak.authenticated) {
      const redirect = location.origin + basePath + to.path;
      const loginUrl = router.app.$keycloak.createLoginUrl({
        idpHint: 'idir',
        redirectUri: redirect
      });
      window.location.replace(loginUrl);
    } else {
      document.title = to.meta.title ? to.meta.title : process.env.VUE_APP_TITLE;
      if (to.query.r && isFirstTransition) {
        router.replace({ path: to.query.r.replace(basePath, '') });
      }
      next();
    }
  });

  router.afterEach(() => {
    isFirstTransition = false;
    NProgress.done();
  });

  return router;
}