Google Authentication/Gapi: Uncaught TypeError: Ca

2019-07-23 07:09发布

Good day to everyone!

I'm creating a web app using Angular.js 1.5, Google Authentication (gapi) and UI router.

My main goal is to initialize GoogleUser at the first place, using resolve method from the main abstract state:

$stateProvider
    .state('app', {
      url:'/',
      templateUrl: 'layout/app-view.html',
      resolve: {
        googleAuth: function (User:UserService) {
          return User.initCurrent();
        }
      }
    });

which at that point works pretty well - it injects UserService, and call its initCurrent() method, that returns a promise which will be fullfiled once current user will be initialized. Here is UserService concise code:

class UserService implements UserInterface {
  public current:GoogleUser;
  private _GoogleAuth:GoogleAuthService;
  private _AppConstants;

  constructor(GoogleAuth:GoogleAuthService) {
    'ngInject';
    this._GoogleAuth = GoogleAuth;
  }

  initCurrent():ng.IPromise<GoogleUser> {
    return this._GoogleAuth.getCurrentUser().then((current) => {
      this.current = current;
      return this.current;
    })
  }
}

In its turn initCurrent() receive a promise from getCurrentUser() that will be fulfilled once the gapi will be loaded and GoogleAuth instance will be initialized. For that I've created a GoogleAuthService:

class GoogleAuthService implements GoogleAuthServiceInterface {
  public googleAuth;
  private _AppConstants;
  private deferGoogleAuthInit:ng.IDeferred<any>;

  constructor(AppConstants, $log, $q:ng.IQService) {
    'ngInject';
    this._AppConstants = AppConstants;
    this.deferGoogleAuthInit = $q.defer();
    gapi.load('auth2', () => {
      gapi.auth2.init({
        client_id: AppConstants.googleClientId,
        cookiepolicy: 'single_host_origin'
      }).then((googleAuth) => {
        this.googleAuth = googleAuth;
        this.deferGoogleAuthInit.resolve('GoogleAuth Initialized');
      })
    });
  }
  getCurrentUser() {
    return this.deferGoogleAuthInit
      .promise.then(() => {
        return this.googleAuth.currentUser.get()
      })
  }
}

The problem: 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:

Uncaught TypeError: Cannot read property 'postMessage' of null

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?

0条回答
登录 后发表回答