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

Recipe Request: Complex Route Redirection #133

Open
tim-soft opened this issue Oct 14, 2017 · 1 comment
Open

Recipe Request: Complex Route Redirection #133

tim-soft opened this issue Oct 14, 2017 · 1 comment

Comments

@tim-soft
Copy link

Let's say I have an app with many user roles and many routes. Every route must be protected from unauthorized users and every route must be protected based on a list of permitted user roles.

If I'm understanding the docs correctly, a route is always evaluated starting from the root '/' and cascades down until the desired route is found.

What would be a good strategy for protecting a really large amount of routes for a large amount of reasons? My current plan is to add a function into every route which checks the user. This method feels a little tedious with a more complex app. Is there a more pragmatic pattern with Universal Router?

@frenzzy
Copy link
Member

frenzzy commented Oct 20, 2017

You can use middleware route like so (jsfiddle playground):

import UniversalRouter from 'universal-router';
import createBrowserHistory from 'history/createBrowserHistory';

const router = new UniversalRouter([
  { path: '', action: () => ({ content: 'Home Page' }) },
  { path: '/login', action: () => ({ content: 'Login Page' }) },
  { // middleware route
    path: '', // or '/admin' if you need to protect only admin routes
    action(context) {
      if (!context.user) {
        return { redirect: '/login', from: context.pathname  };
      }
      if (context.user.role !== 'Admin') {
        return { content: 'Access denied!' };
      }
      return context.next(); // go to child routes
    },
    children: [
      { path: '/protected', action: () => ({ content: 'Protected Page' }) },
      // ... (a lot of routes)
    ],
  },
]);

const history = createBrowserHistory();
function render(location) {
  router.resolve({
    pathname: location.pathname,
    user: null, // { name: 'Jhon', role: 'Guest' },
  }).then(page => {
    if (page.redirect) {
      history.push(page.redirect, { from: page.from });
    } else {
      document.body.innerHTML = page.content;
    }
  });
}

history.listen(render);
render(history.location); // initial render

or any other technique from redirects recipe

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

No branches or pull requests

2 participants