React Routes重定向到Auth链接的外部URL

问题描述 投票:0回答:1

我有一个代码示例,我试图重定向尚未登录的用户。我正在使用外部身份验证,以便通过第三方身份验证系统将用户发送到登录。我知道react-routes有Redirect选项,我知道它们只重定向到路径名。有没有办法让它重新定向发生在window.assign,它将用户立即重定向到另一个页面?

先感谢您!

const ProtectedRoute = (auth, component: Component, ...rest) => {
  return <Route
    {...rest}
    render={props => auth.isAuthenticated()
      ? <Component {...props} />
      : <Redirect
        to={{
          pathname: 'http://example.com/',
          state: { from: props.location },
        }}
      />
    }
  />;
};
reactjs react-router
1个回答
2
投票

您可以评估组件的componentDidMount钩子中的日志记录道具,并使用window.location重定向到完全不同的URL。例如:

componentDidMount() {
    if(auth.isAuthenticated()) {
      return <Component {...props} />
    }
    window.location.assign('http://example.com/');
}
© www.soinside.com 2019 - 2024. All rights reserved.