我正在构建与电子商务相关的产品,有两种类型的用户(客户和卖家)。两种类型的用户都有自己的主页(彼此完全不同)。现在,如果客户已登录,我想在客户主页上路由/如果卖家帐户已登录,则需要在卖家主页上。否则它应该路由到登录页面。如何使用react-router
和this boilerplate?
我尝试按照以下方式执行操作(但不起作用):
{
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);
},
},
答案 0 :(得分:0)
我正在使用React Boilerplate做类似的事情。这是它的要点:
在routes.js中定义onEnter
函数
onEnter:redirectToLogin, 路径:'/ account', 名称:'帐户', getComponent(nextState,cb){...
routes.js
导出默认函数createRoutes(store){ //使用getHooks工厂创建可重用的异步注入器 const {injectSagas,redirectToLogin,redirectToDashboard} = getHooks(store);
在app/utils/hooks.js
中创建重定向功能。这是您根据用户角色重定向的地方。
导出功能redirectToLogin(store){ //设置路径名以启用重定向 的setpath(); return(nextState,replace)=> { if(!user(store)){ 更换({ 路径名:'/', state:{nextPathname:nextState.location.pathname} }); } else { if(user(store).account_status ==='closed'){ 更换({ pathname:'/ closedaccount', state:{nextPathname:nextState.location.pathname} }); } } }; }
从app/utils/hooks.js
导出函数getHooks(store){ 返回{ injectReducer:injectAsyncReducer(store), injectSagas:injectAsyncSagas(store), redirectToLogin:redirectToLogin(store),
答案 1 :(得分:0)
我找到了一篇文章here,我在这里写的是要点。 您可以像这样添加道具
<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 =&#39; /&#39;上呈现
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
这实质上增加了一个网关检查,可以对所有容器起作用,并根据您的角色指导您。此外,如果您以某种方式查找错误的网址,它将不允许您访问。