async componentDidMount()

in ui-react/src/components/App.tsx [46:91]


  async componentDidMount() {
    console.log("componentDidMount");
    Hub.listen('auth', async ({payload: {event, data}}) => {
      switch (event) {
        case 'cognitoHostedUI':
          let user = await this.getUser();
          // workaround for FF bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1422334
          // eslint-disable-next-line
          // noinspection SillyAssignmentJS
          window.location.hash = window.location.hash;
          this.setState({authState: 'signedIn', user: user});
          break;
        case 'cognitoHostedUI_failure':
          this.setState({authState: 'signedOut', user: null, error: data});
          break;
        default:
          break;
      }
    });

    // if the URL contains ?identity_provider=x, and the user is signed out, we redirect to the IdP on load
    const urlParams = new URLSearchParams(window.location.search);
    const idpParamName = 'identity_provider';
    const idp = urlParams.get(idpParamName);

    try {
      let user = await this.getUser();

      // remove identity_provider query param (not needed if signed in successfully)
      if (idp) {
        urlParams.delete(idpParamName);
        const params = urlParams.toString();
        window.history.replaceState(null, null, window.location.pathname + (params ? '?' + params : ''));
      }

      this.setState({authState: 'signedIn', user: user});
    } catch (e) {
      // user is not authenticated, and we have an IdP in the request
      if (e === 'not authenticated' && idp) {
        await Auth.federatedSignIn({customProvider: idp});
      } else {
        console.warn(e);
        this.setState({authState: 'signedOut', user: null});
      }
    }
  }