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 gating to routes #9

Open
kirillku opened this issue Mar 29, 2022 · 1 comment
Open

Add gating to routes #9

kirillku opened this issue Mar 29, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@kirillku
Copy link
Owner

kirillku commented Mar 29, 2022

  1. With filter
const HomeRoute = createRoute({
  path: "/",
  // does not trigger any route events if `filter` is falsy
  filter: $isAuthenticated,
});

// Show some other page
const LoginRoute = createRoute({
  path: "/",
  filter: $isAuthenticated.map(isAuthenticated => !isAuthenticated),
});

const App = () => (
  <Switch>
    <LoginRoute><LoginPage /></LoginRoute>
    <HomeRoute><HomePage /></HomeRoute>
  </Switch>
);

// Or redirect to some other page
const NotAuthenticatedRoute = createRoute({
  path: "/",
  filter: $isAuthenticated.map(isAuthenticated => !isAuthenticated),
});

const LoginRoute = createRoute({
  path: "/login",
});

sample({
  source: NotAuthenticatedRoute.open,
  target: LoginRoute.navigate
});
  1. Custom complex logic on top of filter (handling of auth, access control, and feature flags at the same time)
const createAccessRoute = ({ path, $hasFeatureFlag, $hasAccess }) => {
  const $isNotAuthenticated = $isAuthenticated.map((isAuthenticated) => !isAuthenticated);
  const NoAuthenticatedRoute = createRoute({
    path,
    filter: $isNotAuthenticated,
  });

  const $hasNoFeatureFlag = combine(
    $isAuthenticated,
    $hasFeatureFlag,
    (isAuthenticated, hasFeatureFlag) => isAuthenticated && !hasFeatureFlag
  );
  const NoFeatureFlagRoute = createRoute({
    path,
    filter: $hasNoFeatureFlag,
  });

  const $hasNoAccess = combine(
    $isAuthenticated,
    $hasFeatureFlag,
    $hasAccess,
    (isAuthenticated, hasFeatureFlag, hasAccess) => isAuthenticated && hasFeatureFlag && !hasAccess
  );
  const NoAccessRoute = createRoute({
    path,
    filter: $hasNoAccess,
  });

  $canEnterRoute = combine(
    $isAuthenticated,
    $hasFeatureFlag,
    $hasAccess,
    (isAuthenticated, hasFeatureFlag, hasAccess) => isAuthenticated && hasFeatureFlag && hasAccess
  );
  const FeatureRoute = createRoute({
    path,
    filter: $canEnterRoute,
  });

  return (props) => (
    <Switch>
      <NoAuthenticatedRoute>
        <LoginPage />
      </NoAuthenticatedRoute>
      <NoFeatureFlagRoute>
        <NotFoundPage />
      </NoFeatureFlagRoute>
      <NoAccessRoute>
        <NoAccessPage />
      </NoAccessRoute>
      <FeatureRoute {...props} />
    </Switch>
  );
};

const SettingsRoute = createAccessRoute({
  path: "/settings",
  $hasFeatureFlag: hasFeatureFlag("SETTINGS"),
  $hasAccess: hasAccess("SETTINGS"),
});
@kirillku kirillku added the enhancement New feature or request label Mar 29, 2022
@kirillku
Copy link
Owner Author

Possible approach to the same problem in atomic-router: atomic-router/atomic-router#10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant