0

我有一个应用程序需要通过身份验证和对身份验证服务器 (AS) 的 api 调用进行身份验证。(AS) 将使用我需要设置一些浏览器 cookie 的信息重定向到我的应用程序,这将根据他/她的权限级别影响用户可以看到的内容。

但是,我无法让我的应用程序检测 cookie 的变化并相应地呈现。我需要手动点击刷新按钮来强制组件查看 cookie 并提取信息。

我的问题是:当我从身份验证服务器重定向回我的反应应用程序时,为什么没有调用诸如 render()、componentDidMount() 之类的方法?

应用程序.js:

componentDidMount() {
    console.log('App mounted!');
    const env = process.env.NODE_ENV;
    const code = this.getParameterByName('code');
    const stateID = this.getParameterByName('state');
    const authentication = new Authentication();
    const authorization = new Authorization();

    const sid = Cookies.get('sid'); 

    console.log('sid: ', sid);

    if (sid === undefined) {
      if (code === null || stateID === null) {
        authentication.signin(expressURL, env);
      } else {
        authentication.handleIDToken(expressURL, code, stateID).then(reply => {
          // console.log('Reply: ', reply);

          // let dt = new Date(reply.expiry * 1000).toUTCString();
          Cookies.set('userRole', reply.Role, {path: '/', expiry: new Date(reply.expiry * 1000).toUTCString()});
          Cookies.set('userName', reply.Name, {path: '/', expiry: new Date(reply.expiry * 1000).toUTCString()});
          Cookies.set('sid', reply.SID, {path: '/', expiry: new Date(reply.expiry * 1000).toUTCString()});
        });
      }
    }

    if (sid !== undefined && Cookies.get('apiToken') === undefined) {
      authorization.setDjangoApiToken(expressURL).then(response => {
        if (response === 200) {
          this.initialize();  // Populates my redux stores
        } else {
          this.setState({
            error: response
          });
        }
      });
    }
  }

还是我误解了一些 React 生命周期?

我正在使用 React15 和 js-cookies

*** 我有一个 Express.js 后端,它执行对身份验证服务器的这些调用。

干杯

4

1 回答 1

0

这里有几个选项:

  1. 强制您的应用重新加载window.location.reload();
  2. 将一些道具传递给componentDidUpdate()您的 App.js 并评估是否已设置 cookie。
于 2019-07-02T03:10:31.377 回答