私がここに書いている記事の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} />
ようにあなたのコンポーネントに小道具を追加し、これは、本質的に追加されます
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にヒットした場合、アクセスを許可しません。
ただし、この場合は同じルートにはなりません。あなたにもう一度説明しましょう:私は2つのホームページを持っています。 1つは売り手のためであり、もう1つは顧客のためのものです。ユーザーがログインした後、対応するホームページにルーティングする必要があります。しかし、URLは同じでなければなりません。 '/' facebookを例にしましょう。着陸/ホームページは、loggedInとnot-loggedInのアカウントでは異なります。 –
ああ、もしあなたが同じURLを望むなら、この解決法はそれができないようにします。 /顧客または顧客の役割に基づく/売り手。 – Kunal