polygon-software / flox

2 stars 1 forks source link

application specific: add paths per role #465

Open github-actions[bot] opened 1 year ago

github-actions[bot] commented 1 year ago

application specific: add paths per role

switch (user.role) {

case ROLE.ADMIN:

return ROUTES.CUSTOMERS;

case ROLE.USER:

return ROUTES.CUSTOMERS.path + '/' + user.username;

default:

return ROUTES.LOGIN;

}

eslint-disable-next-line sonarjs/cognitive-complexity,consistent-return

https://github.com/polygon-software/flox/blob/4d186fe59f2032a9ae87c9743e434fb43c7c4b76/frontend/src/boot/router.ts#L33


import { boot } from 'quasar/wrappers';
import { Router, RouteRecordRaw } from 'vue-router';

import { isModuleActive } from '../flox';
import { MODULES } from '../flox/enum/MODULES';
import UserEntity from '../flox/modules/auth/entities/user.entity';
import { fetchMyUser } from '../flox/modules/auth/services/user.service';
import { useAuthStore } from '../flox/modules/auth/stores/auth.store';
import ROUTES, { CONSTRAINED_ROUTES, PUBLIC_ROUTES } from '../router/routes';

// eslint-disable-next-line import/no-mutable-exports
let routerInstance: Router;

/**
 * Returns the component of the dashboard for the currently logged-in user
 *
 * @param user - the user, if any
 * @param $authStore - authentication store
 * @returns the layout component
 */
function getUserRoleRoute(
  user: UserEntity | null,
  $authStore: ReturnType<typeof useAuthStore>
): RouteRecordRaw {
  // Non-logged in: Redirect to log in
  if (!user) {
    $authStore.setCognitoUser(undefined);
    $authStore.setUserSession(undefined);
    return ROUTES.LOGIN;
  }

  return ROUTES.HOME;
  // TODO application specific: add paths per role
  // switch (user.role) {
  //   case ROLE.ADMIN:
  //     return ROUTES.CUSTOMERS;
  //   case ROLE.USER:
  //     return ROUTES.CUSTOMERS.path + '/' + user.username;
  //   default:
  //     return ROUTES.LOGIN;
  // }
}

export default boot(({ router }) => {
  const $authStore = useAuthStore();
  routerInstance = router;
  // eslint-disable-next-line sonarjs/cognitive-complexity,consistent-return
  router.beforeEach(async (to) => {
    // Verify valid authentication
    const { loggedIn } = $authStore;

    // TODO: Add as part of sharing module
    // Case 1: trying to access non-public route while not logged in

c34afcdbe6cf8bf4dc8cd9656b2379056bd551f0