Commit 03d0c8a5 authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

Implement global API interceptor for graceful session expiry handling

parent a3bbd310
Loading
Loading
Loading
Loading
+27 −0
Changes for js-apps/frontend/src/js/app-container.js: 27 added lines, 0 removed lines.
Original line number Original line Diff line number Diff line
@@ -172,6 +172,33 @@ class AppContainer extends Component {
    this.meepGeoDataApi = new meepGisEngineRestApiClient.GeospatialDataApi();
    this.meepGeoDataApi = new meepGisEngineRestApiClient.GeospatialDataApi();
    this.meepMonEngineApi = new meepMonEngineRestApiClient.PodStatesApi();
    this.meepMonEngineApi = new meepMonEngineRestApiClient.PodStatesApi();


    // Intercept 401 Unauthorized responses to gracefully handle session expiry
    const addAuthInterceptor = (client) => {
      const instance = client.ApiClient.instance;
      const originalCallApi = instance.callApi.bind(instance);
      instance.callApi = (path, httpMethod, pathParams, queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, callback) => {
        const wrappedCallback = (error, data, response) => {
          if (error && error.status === StatusCodes.UNAUTHORIZED) {
            if (this.props.signInStatus === STATUS_SIGNED_IN) {
              this.logout();
              this.props.changeCurrentDialog(DIALOG_SESSION_TERMINATED);
            }
          }
          if (callback) {
            callback(error, data, response);
          }
        };
        return originalCallApi(path, httpMethod, pathParams, queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, wrappedCallback);
      };
    };

    addAuthInterceptor(meepAuthSvcRestApiClient);
    addAuthInterceptor(meepPlatformCtrlRestApiClient);
    addAuthInterceptor(meepSandboxCtrlRestApiClient);
    addAuthInterceptor(meepMetricsEngineRestApiClient);
    addAuthInterceptor(meepGisEngineRestApiClient);
    addAuthInterceptor(meepMonEngineRestApiClient);

    // Initialize undefined states
    // Initialize undefined states
    if (this.props.terminationInProgressCount === undefined) {
    if (this.props.terminationInProgressCount === undefined) {
      this.props.changeTerminationInProgressCount(-1);
      this.props.changeTerminationInProgressCount(-1);
+27 −0
Changes for js-apps/frontend/src/js/containers/app-container.js: 27 added lines, 0 removed lines.
Original line number Original line Diff line number Diff line
@@ -172,6 +172,33 @@ class AppContainer extends Component {
    this.meepGeoDataApi = new meepGisEngineRestApiClient.GeospatialDataApi();
    this.meepGeoDataApi = new meepGisEngineRestApiClient.GeospatialDataApi();
    this.meepMonEngineApi = new meepMonEngineRestApiClient.PodStatesApi();
    this.meepMonEngineApi = new meepMonEngineRestApiClient.PodStatesApi();


    // Intercept 401 Unauthorized responses to gracefully handle session expiry
    const addAuthInterceptor = (client) => {
      const instance = client.ApiClient.instance;
      const originalCallApi = instance.callApi.bind(instance);
      instance.callApi = (path, httpMethod, pathParams, queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, callback) => {
        const wrappedCallback = (error, data, response) => {
          if (error && error.status === StatusCodes.UNAUTHORIZED) {
            if (this.props.signInStatus === STATUS_SIGNED_IN) {
              this.logout();
              this.props.changeCurrentDialog(DIALOG_SESSION_TERMINATED);
            }
          }
          if (callback) {
            callback(error, data, response);
          }
        };
        return originalCallApi(path, httpMethod, pathParams, queryParams, collectionQueryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, wrappedCallback);
      };
    };

    addAuthInterceptor(meepAuthSvcRestApiClient);
    addAuthInterceptor(meepPlatformCtrlRestApiClient);
    addAuthInterceptor(meepSandboxCtrlRestApiClient);
    addAuthInterceptor(meepMetricsEngineRestApiClient);
    addAuthInterceptor(meepGisEngineRestApiClient);
    addAuthInterceptor(meepMonEngineRestApiClient);

    // Initialize undefined states
    // Initialize undefined states
    if (this.props.terminationInProgressCount === undefined) {
    if (this.props.terminationInProgressCount === undefined) {
      this.props.changeTerminationInProgressCount(-1);
      this.props.changeTerminationInProgressCount(-1);