<commit_before>import { module } from 'angular';
import { IModalService, IModalStackService } from 'angular-ui-bootstrap';
import { Observable, Subscription } from 'rxjs';

import { SETTINGS } from 'core/config/settings';
import { IDeckRootScope } from 'core/domain';

import { AUTHENTICATION_SERVICE, AuthenticationService } from './authentication.service';
import { REDIRECT_SERVICE, RedirectService } from './redirect.service';

interface IAuthResponse {
  username: string;
  roles?: string[];
}

export class AuthenticationInitializer {

  private userLoggedOut = false;
  private visibilityWatch: Subscription = null;

  constructor(private $location: ng.ILocationService,
              private $rootScope: IDeckRootScope,
              private $http: ng.IHttpService,
              private $uibModal: IModalService,
              private $uibModalStack: IModalStackService,
              private redirectService: RedirectService,
              private authenticationService: AuthenticationService) {
    'ngInject';
  }

  private checkForReauthentication(): void {
    this.$http.get(SETTINGS.authEndpoint)
      .then((response: ng.IHttpPromiseCallbackArg<IAuthResponse>) => {
        if (response.data.username) {
          this.authenticationService.setAuthenticatedUser({
            name: response.data.username,
            authenticated: false,
            roles: response.data.roles
          });
          this.$uibModalStack.dismissAll();
          this.visibilityWatch.unsubscribe();
        }
      });
  }

  private loginNotification(): void {
    this.authenticationService.authenticationExpired();
    this.userLoggedOut = true;
    this.openLoggedOutModal();

    this.visibilityWatch = Observable.fromEvent(document, 'visibilitychange')
      .subscribe(() => {
        if (document.visibilityState === 'visible') {
          this.checkForReauthentication();
        }
      });
  }

  private openLoggedOutModal(): void {
    this.$uibModal.open({
      templateUrl: require('./loggedOut.modal.html'),
      controller: 'LoggedOutModalCtrl as ctrl',
      size: 'squared'
    });
  }

  private loginRedirect(): void {
    const callback: string = encodeURIComponent(this.$location.absUrl());
    this.redirectService.redirect(`${SETTINGS.gateUrl}/auth/redirect?to=${callback}`);
  }<commit_msg>fix(core): Fix unhandled rejection on auth recheck (#4335)<commit_after>import { module } from 'angular';
import { IModalService, IModalStackService } from 'angular-ui-bootstrap';
import { Observable, Subscription } from 'rxjs';

import { SETTINGS } from 'core/config/settings';
import { IDeckRootScope } from 'core/domain';

import { AUTHENTICATION_SERVICE, AuthenticationService } from './authentication.service';
import { REDIRECT_SERVICE, RedirectService } from './redirect.service';

interface IAuthResponse {
  username: string;
  roles?: string[];
}

export class AuthenticationInitializer {

  private userLoggedOut = false;
  private visibilityWatch: Subscription = null;

  constructor(private $location: ng.ILocationService,
              private $rootScope: IDeckRootScope,
              private $http: ng.IHttpService,
              private $uibModal: IModalService,
              private $uibModalStack: IModalStackService,
              private redirectService: RedirectService,
              private authenticationService: AuthenticationService) {
    'ngInject';
  }

  private checkForReauthentication(): void {
    this.$http.get(SETTINGS.authEndpoint)
      .then((response: ng.IHttpPromiseCallbackArg<IAuthResponse>) => {
        if (response.data.username) {
          this.authenticationService.setAuthenticatedUser({
            name: response.data.username,
            authenticated: false,
            roles: response.data.roles
          });
          this.$uibModalStack.dismissAll();
          this.visibilityWatch.unsubscribe();
        }
      })
      .catch(() => {});
  }

  private loginNotification(): void {
    this.authenticationService.authenticationExpired();
    this.userLoggedOut = true;
    this.openLoggedOutModal();

    this.visibilityWatch = Observable.fromEvent(document, 'visibilitychange')
      .subscribe(() => {
        if (document.visibilityState === 'visible') {
          this.checkForReauthentication();
        }
      });
  }

  private openLoggedOutModal(): void {
    this.$uibModal.open({
      templateUrl: require('./loggedOut.modal.html'),
      controller: 'LoggedOutModalCtrl as ctrl',
      size: 'squared'
    });
  }

  private loginRedirect(): void {
    const callback: string = encodeURIComponent(this.$location.absUrl());
    this.redirectService.redirect(`${SETTINGS.gateUrl}/auth/redirect?to=${callback}`);
  }
{'fraction_non_alphanumeric': 0.11566213921901528, 'fraction_numerical': 0.0008488964346349745, 'mean_word_length': 3.2845454545454547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399344', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1271, 'n_words': 252}