我试图通过react-router v4
实现受限制的路由,而且我几乎完成了任务,但是我需要处理JWT令牌的验证,并且不知道将该位置放在哪里。在React应用程序中处理JWT身份验证
RestrictedRoute
const RestrictedRoute = (props) => {
const { component, ...otherProps } = props;
return <Route {...otherProps} component={component} />;
};
const mapStateToProps = state => ({
authenticated: state.authentication.session,
});
const branched = branch(
({ authenticated }) => !authenticated,
renderComponent(() => <Redirect to="/login" />),
);
const enhanced = compose(
connect(mapStateToProps),
branched,
)(RestrictedRoute);
export default enhanced;
所以用这个我能如果state.authentication.session
设置为false
将用户重定向到/login
页。到目前为止,我正在检查是否存在jwt
Cookie,并基于此设置此session
。这有一些简单的漏洞,因为您可以添加具有任何值的jwt
Cookie,并且您将被视为已通过身份验证。
我创建了一个端点用于验证我的jwt
令牌并为此创建了一个操作。我的问题是 - 在哪里把这个?
如果我把这个在RestrictedRoute
componentWillMount
和验证的用户试图访问受限制的网页时,他表现出login
页,然后切换到受限制的路径,从终端的响应改变了Redux店。
谢谢为答案。我通过验证服务器上的令牌(正如服务器端渲染)并将数据传递给我的redux商店来处理它。 – Ancinek
欢迎@Ancinek –