0

在 React 中使用 MSAL 2.0,我们可以使用以下方法轻松实现重定向到登录页面

  1. 使用弹窗登录
  2. 使用重定向登录

但是这两种方法都可以在登录/登录按钮操作上执行。

就我而言,我想将我的 React 应用程序直接重定向到登录页面,当 URL 在浏览器中被点击时,即没有按钮操作。

此外,在进行重定向时,我还需要传递我的多个范围,以获得用户的同意。

我做了一些研究,但没有找到任何可行的解决方案。请问有人可以帮我吗?

4

1 回答 1

0

当应用程序启动时,它总是以 app.js 开头。在 app.js 类构造函数中,您可以检查令牌是否有效/无效以及是否已过期。

为了将您的 React 应用程序直接重定向到登录页面,请尝试以下操作:

state = {
  show: false;
}

componentWillReceiveProps(nextProps) {
  if (nextProps.children !== this.props.children) {
    this.checkAuth();
  }
}

componentDidMount() {
  this.checkAuth();
}

checkAuth() {
  Api.checkAuth()
  .then(result => {
    if (result.success) {
      this.setState({ show: true });
    } else {
  return <Redirect to='/login'  />
    }
  });
}
于 2021-10-05T06:09:51.057 回答