0

I am building an e-commerce related product and there are two types of users (customer & seller). Both types of user have their own homepage (totally different to each other). Now I want to route / on customer homepage if customer is logged-in or to seller homepage if seller account is logged-in. Otherwise it should route to landing page. How can I achieve this feature using react-router and this boilerplate?

I tried to do something as follows (but it didn't work):

{
      path: '/',
      name: 'home',
      getComponent(nextState, cb) {
        let HomePagePath = 'containers/HomePage';
        if (customerLoggedIn) {
            HomePagePath = 'containers/CustomerHomePage';
       }
        const importModules = Promise.all([
          import(HomePagePath),
        ]);

        const renderRoute = loadModule(cb);

        importModules.then(([component]) => {
          renderRoute(component);
        });

        importModules.catch(errorLoading);
      },
    }, 
4

2 回答 2

0

我在这里找到了一篇文章我在这里写了它的要点。你可以像这样向你的组件添加一个道具

<Route path="/" component={App}>

//BOD routes
<Route authorisedUsers={['KR']} path="/home" component={HomeContainer} />

//HR routes
<Route authorisedUsers={['HR']} path="/hrhome" component={HRDashboardContainer} />

//common routes    
<Route authorisedUsers={['KR', 'HR']} path="/notes" component={NotesContainer} />

然后在您的组件中添加以下代码,在 path='/' 上呈现

Role based routing react redux
componentDidUpdate() {
  const { 
      children,  //Component to be rendered (HomeContainer if route = '/home')
      pathname: {location},  //location.pathname gives us the current url user is trying to hit. (with react router)
      profileId, //profileId required by profile page common to all user journeys.
      role } = this.props; 
  this.reRoute(role, this.props.children, location.pathname, ProfileId)
}

decideRoute(role, ProfileId) { //decide routes based on role
  if(role==='HR')
    return 'hrhome';
  else if(role==='KR')
    return 'home';
  else if(role==='USER'&&ProfileId)
    return 'profile/'+ProfileId;
  else
  return '/error';
}

isAuthorised(authorisedUsers, role) {
  return _.includes(authorisedUsers, role)
}

reRoute(role, children, path, ProfileId) {
  if(role===null && path!=='/') // user hit a different path without being authenticated first
  {
    hashHistory.replace('/');  //this is where we implemented login
    return;
  }
  let route = this.decideRoute(role, ProfileId)  //if role has already been fetched from the backend, use it to decide the landing page for each role.
  if(children)  // if we already are on one of the user journey screens ...
  {
    const authorisedUsers = children.props.route.authorisedUsers 
    if(!this.isAuthorised(authorisedUsers,role)) //... and the user is not allowed to view this page...
    hashHistory.replace(`/${route}/`);  //... redirect him to the home page corresponding to his role.
  }
  else
  hashHistory.replace(`/${route}/`);  // if the user has just logged in(still on / page or login page), and we know his role, redirect him to his home page.
}//if none of these holds true, user is allowed to go ahead and view the page

这实质上添加了一个网关检查,该检查适用于您的所有容器,并将根据您的角色指导您。此外,如果您以某种方式点击了错误的 url,它将不允许您访问。

于 2017-04-24T17:54:38.850 回答
0

我正在使用 React Boilerplate 做类似的事情。这是它的要点:

  1. onEnter在 routes.js 中定义一个函数

    onEnter:redirectToLogin,路径:'/account',名称:'account',getComponent(nextState, cb) { ...

  2. 导入函数routes.js

    export default function createRoutes(store) { // 使用 getHooks 创建可重用的异步注入器 factory const { injectSagas, redirectToLogin, redirectToDashboard } = getHooks(store);

  3. 在 中创建您的重定向功能app/utils/hooks.js。这是您根据用户角色重定向的地方。

    export function redirectToLogin(store) { // 设置路径名以启用重定向 setPath(); return (nextState, replace) => { if (!user(store)) { replace({ pathname: '/', state: { nextPathname: nextState.location.pathname } }); } else { if (user(store).account_status === 'closed') { replace({ pathname: '/closedaccount', state: { nextPathname: nextState.location.pathname } }); } } }; }

  4. 导出重定向功能app/utils/hooks.js

    导出函数 getHooks(store) { return { injectReducer:injectAsyncReducer(store),injectSagas:injectAsyncSagas(store),redirectToLogin:redirectToLogin(store),

于 2017-02-27T12:03:57.167 回答