Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as part of sharing module #344

Open
github-actions bot opened this issue Jul 6, 2022 · 0 comments
Open

Add as part of sharing module #344

github-actions bot opened this issue Jul 6, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Jul 6, 2022

Add as part of sharing module

Case 1: trying to access non-public route while not logged in

if (

!loggedIn &&

!PUBLIC_ROUTES.some((publicRoute) => {

return publicRoute.path === to.path;

})

) {

// Check if it is a public link

if (to.query.key) {

const validKey = await fetchKeyValid(to.path, to.query.key as string);

if (validKey) {

return true;

}

}

return ROUTES.LOGIN;

} else

// TODO: Add as part of sharing module

import { boot } from 'quasar/wrappers';
import ROUTES, { CONSTRAINED_ROUTES, PUBLIC_ROUTES } from '../router/routes';
import { Router } from 'vue-router';
import { root } from 'src/store';
import { User } from 'src/data/types/User';
import { Context, Module } from 'vuex-smart-module';
import AuthState from 'src/store/authentication/state';
import AuthGetters from 'src/store/authentication/getters';
import AuthActions from 'src/store/authentication/actions';
import AuthMutations from 'src/store/authentication/mutations';
import { fetchMyUser } from 'src/helpers/data/fetch-helpers';
import {isModuleActive} from 'src/flox';
import {MODULES} from 'src/flox/MODULES';

let routerInstance: Router;

export default boot(({ router, store }) => {
  // Get auth module within store (useAuth not working here)
  const $authStore = root.context(store).modules.authModule;
  routerInstance = router;
  // eslint-disable-next-line sonarjs/cognitive-complexity
  router.beforeEach(async (to) => {
    // Verify valid authentication
    const loggedIn = $authStore.getters.getLoggedInStatus();

    // TODO: Add as part of sharing module
    // Case 1: trying to access non-public route while not logged in
    // if (
    //   !loggedIn &&
    //   !PUBLIC_ROUTES.some((publicRoute) => {
    //     return publicRoute.path === to.path;
    //   })
    // ) {
    //   // Check if it is a public link
    //   if (to.query.key) {
    //     const validKey = await fetchKeyValid(to.path, to.query.key as string);
    //     if (validKey) {
    //       return true;
    //     }
    //   }
    //   return ROUTES.LOGIN;
    // } else
    if (loggedIn) {
      const user = await fetchMyUser();

      // Case 2: going to login when logged in, or to default path '/'
      if (!user || to.path === ROUTES.LOGIN.path || to.path === '/') {
        return getUserRoleRoute(user, $authStore);
      }

      // Case 3: role module is active and route has some constraints
      if(isModuleActive(MODULES.ROLES)){
        const matchingConstrainedRoute = CONSTRAINED_ROUTES.find(
          (constrainedRoute) => constrainedRoute.path === to.path
        );
        if (matchingConstrainedRoute) {
          const hasFullAccess = matchingConstrainedRoute.allowedRoles.includes(
            user.role
          );
          if (!hasFullAccess) {
            return getUserRoleRoute(user, $authStore);
          }
        }
      }
    } else {
      // Default case: disallow access if not public
      if(!PUBLIC_ROUTES.some((publicRoute) => publicRoute.path === to.path)){
        return ROUTES.LOGIN
      }
    }
  });
});

// Router instance for use in Vue components
export { routerInstance };

/**
 * Returns the component of the dashboard for the currently logged in user
 * @param {User|null} user - the user, if any
 * @param {Context<Module<AuthState, AuthGetters, AuthMutations, AuthActions, Record<string, any>>>} $authStore - authentication store
 * @returns {any} - the layout component
 */
function getUserRoleRoute(
  user: User | null,
  $authStore: Context<
    Module<
      AuthState,
      AuthGetters,
      AuthMutations,
      AuthActions,
      Record<string, any>
      >
    >
) {
  // Non-logged in: Redirect to login
  if (!user) {
    $authStore.mutations.setCognitoUser(undefined);
    $authStore.mutations.setUserSession(undefined);
    return ROUTES.LOGIN;
  }

  return ROUTES.SAMPLE
  // 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;
  // }
}

455451358a2a89c7c0400b7e759acd78834fdec6

@github-actions github-actions bot added the todo label Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants