title: Google Authentication/Gapi: Uncaught TypeError: Cannot read property 'postMessage' of null
body: <p><em>Good day to everyone!</em></p>
  
  <p>I'm creating a web app using <strong>Angular.js 1.5</strong>, <strong>Google Authentication (gapi)</strong> and <strong>UI router</strong>.</p>
  
  <p>My main goal is to initialize <strong>GoogleUser</strong> at the first place, using <strong>resolve method</strong> from the main abstract state:</p>
  
  <pre><code>$stateProvider
      .state('app', {
        url:'/',
        templateUrl: 'layout/app-view.html',
        resolve: {
          googleAuth: function (User:UserService) {
            return User.initCurrent();
          }
        }
      });
  </code></pre>
  
  <p>which at that point works pretty well - it injects <strong>UserService</strong>, and call its <strong>initCurrent()</strong> method, that returns a <strong>promise</strong> which will be fullfiled once current user will be initialized. Here is <strong>UserService concise code</strong>:</p>
  
  <pre><code>class UserService implements UserInterface {
    public current:GoogleUser;
    private _GoogleAuth:GoogleAuthService;
    private _AppConstants;
  
    constructor(GoogleAuth:GoogleAuthService) {
      'ngInject';
      this._GoogleAuth = GoogleAuth;
    }
  
    initCurrent():ng.IPromise&lt;GoogleUser&gt; {
      return this._GoogleAuth.getCurrentUser().then((current) =&gt; {
        this.current = current;
        return this.current;
      })
    }
  }
  </code></pre>
  
  <p>In its turn <strong>initCurrent()</strong> receive a promise from <strong>getCurrentUser()</strong> that will be fulfilled once the <strong>gapi</strong> will be loaded and <strong>GoogleAuth</strong> instance will be initialized. For that I've created a <strong>GoogleAuthService</strong>:</p>
  
  <pre><code>class GoogleAuthService implements GoogleAuthServiceInterface {
    public googleAuth;
    private _AppConstants;
    private deferGoogleAuthInit:ng.IDeferred&lt;any&gt;;
  
    constructor(AppConstants, $log, $q:ng.IQService) {
      'ngInject';
      this._AppConstants = AppConstants;
      this.deferGoogleAuthInit = $q.defer();
      gapi.load('auth2', () =&gt; {
        gapi.auth2.init({
          client_id: AppConstants.googleClientId,
          cookiepolicy: 'single_host_origin'
        }).then((googleAuth) =&gt; {
          this.googleAuth = googleAuth;
          this.deferGoogleAuthInit.resolve('GoogleAuth Initialized');
        })
      });
    }
    getCurrentUser() {
      return this.deferGoogleAuthInit
        .promise.then(() =&gt; {
          return this.googleAuth.currentUser.get()
        })
    }
  }
  </code></pre>
  
  <p><strong>The problem:</strong> When I'm not using resolve method to initialize user, and do all the same via console: getting UserService instance, initCurrentUser and etc. everything works fine: signIn/signOut, getting current User and etc. 
  But when I initialize current User inside resolve method, attempt to signOut  causes the following error message:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'postMessage' of null</p>
  </blockquote>
  
  <p>But at the same time all services are injecting properly and I'm receiving a proper user instance, although I cannot reSignIn it. What would you suggest?</p>