我在应用程序(NodeJS)的后端部分执行 RBAC 和授权,并且从未真正关心在 UI 上强制执行授权。
但是,假设我有以下 React Router 4 路由:
<Switch>
<Route exact path="/books/all" component={BookList} />
<Route exact path="/books/new" component={BookNew} />
<Route exact path="/books/:bookId" component={BookDetails} />
<Route exact path="/books/:bookId/edit" component={BookEdit} />
</Switch>
而且我想确保如果登录用户访问了一本不是他的书,他将无法呈现路线/books/<not my book>/edit。我可以通过在ComponentDidMount()函数中实现一个简单的检查来做到这一点:
checkAuthorisation = userId => {
if (this.props.authenticated._id !== userId) {
this.props.history.push("/books/all");
}
};
但我想知道在 ReactJS 中是否有更好的方法/设计模式?我想知道是否bookId完全从路线中删除并只推送类似editand的propsbookId:
<Route exact path="/books/edit" component={BookEdit} />