{"size": 1013, "ext": "js", "max_stars_repo_path": "source/authentication/authentication.httpinterceptor.js", "max_stars_repo_name": "cs1193/zeplin.ui", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "source/authentication/authentication.httpinterceptor.js", "max_issues_repo_name": "cs1193/zeplin.ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "source/authentication/authentication.httpinterceptor.js", "max_forks_repo_name": "cs1193/zeplin.ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.325, "max_line_length": 80, "alphanum_fraction": 0.701875617}
/* @flow */

import AuthenticationService from './authentication.service';

export default class AuthenticationHttpInterceptor {
  static NAME: string = 'AuthenticationHttpInterceptor';

  /* @ngInject */
  static factory(
    $rootScope: any,
    AuthenticationService: AuthenticationService,
  ): AuthenticationHttpInterceptor {
    return new AuthenticationHttpInterceptor($rootScope, AuthenticationService);
  }

  /* @ngInject */
  constructor($rootScope: any, AuthenticationService: AuthenticationService) {
    this.$rootScope = $rootScope;
    this.AuthenticationService = AuthenticationService;
  }

  request = (config: any) => {
    config.headers = config.headers || {};
    let accessToken = this.AuthenticationService.getAccessToken();

    if (accessToken) {
      config.headers['x-access-token'] = accessToken;
    }

    return config;
  }

  responseError = (response) => {
    if (response.status === 401) {
      this.AuthenticationService.redirectToLogin();
    }
    return response;
  }
}
