Google Sign In – Signing out on refresh

All we need is an easy explanation of the problem, so here it is.

I have the following set up:

.service('googleService', ['$q', function ($q) {
    var self = this;

    this.load = function(){
        var deferred = $q.defer();
        gapi.load('auth2', function(){
            var auth2 = gapi.auth2.init();
            auth2.then(function(){ deferred.resolve(); });
            addAuth2Functions(auth2);
        });
        return deferred.promise;
    };

    function addAuth2Functions(auth2) {

        self.isSignedIn = function(){
            return auth2.isSignedIn.get();
        }

        self.signOut = function(){
            var deferred = $q.defer();
            auth2.signOut().then(deferred.resolve, deferred.reject);
            return deferred.promise;
        };

        self.getProfile = function() {
            if(auth2.isSignedIn.get()) return { signed_in: true, access_token: auth2.currentUser.get().Zi.id_token,profile: auth2.currentUser.get().getBasicProfile() };
            else return { signed_in: false };
        }

    }

}])

.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/cloud');

    var guest = ['$q', '$rootScope', '$stateParams', 'googleService', function ($q, $rootScope, $stateParams, googleService) { 

        var deferred = $q.defer(); 

        googleService.load().then(function(){ 
            $q.when(googleService.isSignedIn()).then(function(r){ 
                if(r) deferred.reject(); 
                else deferred.resolve(); 
            }) 
        }); 

        return deferred.promise; 
    }];

    var authenticated = ['$q', '$rootScope', '$stateParams', 'googleService', function ($q, $rootScope, $stateParams, googleService) { 

        var deferred = $q.defer(); 

        googleService.load().then(function(){ 
            $q.when(googleService.getProfile()).then(function(p) { 
                if(p.signed_in) { 
                    deferred.resolve(); 
                    localStorage['access_token'] = p.access_token;
                    $rootScope.profile = p.profile; 
                } else deferred.reject(); 
            }) 
        }); 

        return deferred.promise; 
    }];

    $stateProvider

    .state('login', {
        url: '/',
        views: { 'main': { templateUrl: 'pages/templates/login.html', controller: 'login' } },
        resolve: { authenticated: guest }
    })

    .state('cloud', {
        url: '/cloud',
        views: { 'main': { templateUrl: 'pages/templates/cloud.html', controller: 'cloud' } },
        resolve: { authenticated: authenticated }
    })

})

.controller('login', ['$rootScope', '$scope', '$q', '$state', 'googleService', function ($rootScope, $scope, $q, $state, googleService) {
    $scope.options = { 'onsuccess': function(response) { $state.go('cloud'); } }
}])

.controller('cloud', ['$rootScope', '$scope', '$timeout', '$http', '$httpParamSerializerJQLike', function ($rootScope, $scope, $timeout, $http, $httpParamSerializerJQLike) { 

}]);

Basically what is happening is, when I sign in using the google sign in button, it signs in and googleService.getProfile() says that I am signed in.

However, if I refresh the page, googleService.isSignedIn() returns false.

Can anyone see an issue in why it would be returning false? Is there something else I need to do to make sure google remembers me?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Your main problem appears to be that you’re calling gapi.auth2.init() over and over again via googleService.load().

My suggestion is to store the initialisation promises for re-use instead of creating it many times.

You’ll also need to add a condition to handle an expired access token.

.service('googleService', ['$q', function ($q) {
    const auth2InitPromise = $q(function(resolve) {
        gapi.load('auth2', function() {
            var auth2 = gapi.auth2.init();
            auth2.then(function() {
                resolve();
            });
        })
    });

    this.isSignedIn = function() {
        return auth2InitPromise.then(function() {
            return gapi.auth2.getAuthInstance().isSignedIn.get();
        });
    };

    this.signOut = function() {
        return auth2InitPromise.then(function() {
            const auth2 = gapi.auth2.getAuthInstance();
            return $q(function(resolve, reject) {
                auth2.signOut().then(resolve, reject);
            });
        });
    };

    this.getProfile = function() {
        return this.isSignedIn().then(function(isSignedIn) {
            if (isSignedIn) {
                const currentUser = gapi.auth2.getAuthInstance().currentUser.get();
                const authResponse = currentUser.getAuthResponse();
                return $q.when(authResponse.expires_at > Date.now() ? authResponse : currentUser.reloadAuthResponse()).then(function(ar) {
                    return {
                        signed_in: true,
                        access_token: ar.id_token,
                        profile: currentUser.getBasicProfile()
                    }                        
                });
            } else {
                return { signed_in: false };
            }
        });
    };

}])

Each of your service’s methods (isSignedIn, signOut and getProfile) now return a promise that only resolves once the auth2 API is initialised however this only ever happens once now.


For example

var authenticated = ['$q', '$rootScope', '$window', 'googleService', function ($q, $rootScope, $window, googleService) {
    return googleService.getProfile().then(function(p) {
        if (p.signed_in) {
            $window.localStorage.setItem('access_token', p.access_token);
            $rootScope.profile = p.profile;
            return true; // resolvers should always resolve with something   
        } else {
            return $q.reject();
        }
    });
}];

Method 2

You need to include authenticated as a dependency in your cloud controller. Also, in your googleService in your success handler you should defer.resolve() after you do localStorage['access_token'] = p.access_token; and $rootScope.profile = p.profile;

Method 3

Hi Do the following changes to your code and let me know if it works.

.service('googleService', ['$q', function ($q) {
    var self = this;

    this.load = function(){
        gapi.load('auth2', callback);            
    };

    function callback(){
        var deferred = $q.defer();
        gapi.auth2.init()//use your client credentials here
        .then(function(){ deferred.resolve(); });
        return deferred.promise;
    }


    self.isSignedIn=function (auth2){
        return auth2.isSignedIn.get();
    };

    self.signOut =function(auth2){
        var deferred = $q.defer();
        auth2.signOut().then(deferred.resolve, deferred.reject);
        return deferred.promise;
    };

    self.getProfile= function(auth2) {
        if(auth2.isSignedIn.get()) return { signed_in: true, access_token: auth2.currentUser.get().Zi.id_token,profile: auth2.currentUser.get().getBasicProfile() };
        else return { signed_in: false };
    };     


}])

   .config(function($stateProvider, $urlRouterProvider, $locationProvider) {

       $locationProvider.html5Mode(true);
       $urlRouterProvider.otherwise('/cloud');



       var guest = ['$q', '$rootScope', '$stateParams', 'googleService', function ($q, $rootScope, $stateParams, googleService) { 

           var deferred = $q.defer(); 
           googleService.load().then(function(){
               googleService.isSignedIn(gapi.auth2.getAuthInstance()).then(function(r){ 
                   if(r) deferred.reject(); 
                   else deferred.resolve(); 
               });   
           });



           return deferred.promise; 
       }];

       var authenticated = ['$q', '$rootScope', '$stateParams', 'googleService', function ($q, $rootScope, $stateParams, googleService) { 

           var deferred = $q.defer(); 

           googleService.load().then(function(){
               googleService.getProfile(gapi.auth2.getAuthInstance()).then(function(p) { 
                   if(p.signed_in) { 
                       deferred.resolve(); 
                       localStorage['access_token'] = p.access_token;
                       $rootScope.profile = p.profile; 
                   } else deferred.reject(); 
               });   
           });            

           return deferred.promise; 
       }];

       $stateProvider

       .state('login', {
           url: '/',
           views: { 'main': { templateUrl: 'pages/templates/login.html', controller: 'login' } },
           resolve: { authenticated: guest }
       })

       .state('cloud', {
           url: '/cloud',
           views: { 'main': { templateUrl: 'pages/templates/cloud.html', controller: 'cloud' } },
           resolve: { authenticated: authenticated }
       })

   })

   .controller('login', ['$rootScope', '$scope', '$q', '$state', 'googleService', function ($rootScope, $scope, $q, $state, googleService) {
       $scope.options = { 'onsuccess': function(response) { $state.go('cloud'); } }
   }])

   .controller('cloud', ['$rootScope', '$scope', '$timeout', '$http', '$httpParamSerializerJQLike', function ($rootScope, $scope, $timeout, $http, $httpParamSerializerJQLike) { 

   }]);

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply