I am currently working with the setup outlined below:
.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) {
}]);
The issue I am facing is that after signing in using the Google sign-in button, googleService.getProfile()
correctly indicates that I am signed in. However, upon refreshing the page, calling googleService.isSignedIn()
returns false.
Is anyone able to spot a potential reason for this behavior and suggest any additional steps needed to ensure that Google remembers my sign-in status?