我们正在使用ContextAPI来管理用户身份验证状态。 我们要制作专用路由组件。
如果用户未通过身份验证,则专用路由组件将重定向到登录页面。
这是我的代码,但是不起作用。
你能告诉我怎么做吗?
pip
错误消息
const WithPrivateRoute = (WrappedComponent) => {
const hocComponent = ({ ...props }) => (
<WrappedComponent {...props} />;
)
hocComponent.getInitialProps = async ({ res }) => {
const { isAuthenticated } = useContext(AuthContext);
if(!isAuthenticated) {
res?.writeHead(302, {
Location: login,
});
res?.end();
}
// ....
}
const PageA = () => {
}
export default WithPrivateRoute(PageA)