Add a new cookie service and have it properly use permanent cookies when needed.

This commit is contained in:
Joseph Schorr 2013-12-17 15:03:34 -05:00
parent 8725b7a87c
commit cc8e0e5ea5

View file

@ -90,7 +90,28 @@ function getMarkedDown(string) {
// Start the application code itself.
quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'angulartics', 'angulartics.mixpanel', '$strap.directives', 'ngCookies'], function($provide) {
$provide.factory('UserService', ['Restangular', '$cookies', function(Restangular, $cookies) {
$provide.factory('CookieService', ['$cookies', '$cookieStore', function($cookies, $cookieStore) {
var cookieService = {};
cookieService.putPermanent = function(name, value) {
document.cookie = escape(name) + "=" + escape(value) + "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
};
cookieService.putSession = function(name, value) {
$cookies[name] = value;
};
cookieService.clear = function(name) {
$cookies[name] = '';
};
cookieService.get = function(name) {
return $cookies[name];
};
return cookieService;
}]);
$provide.factory('UserService', ['Restangular', 'CookieService', function(Restangular, CookieService) {
var userResponse = {
verified: false,
anonymous: true,
@ -103,7 +124,7 @@ quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'an
var userService = {}
userService.hasEverLoggedIn = function() {
return $cookies.loggedIn == 'true';
return CookieService.get('quay.loggedin') == 'true';
};
userService.load = function(opt_callback) {
@ -122,7 +143,7 @@ quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'an
'$created': new Date()
})
$cookies.loggedIn = 'true';
CookieService.putPermanent('quay.loggedin', 'true');
}
if (opt_callback) {
@ -180,8 +201,8 @@ quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'an
return keyService;
}]);
$provide.factory('PlanService', ['Restangular', 'KeyService', 'UserService', '$cookieStore',
function(Restangular, KeyService, UserService, $cookieStore) {
$provide.factory('PlanService', ['Restangular', 'KeyService', 'UserService', 'CookieService',
function(Restangular, KeyService, UserService, CookieService) {
var plans = null;
var planDict = {};
var planService = {};
@ -201,7 +222,7 @@ quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'an
};
planService.notePlan = function(planId) {
$cookieStore.put('quay.notedplan', planId);
CookieService.putSession('quay.notedplan', planId);
};
planService.handleNotedPlan = function() {
@ -224,8 +245,8 @@ quayApp = angular.module('quay', ['ngRoute', 'restangular', 'angularMoment', 'an
};
planService.getAndResetNotedPlan = function() {
var planId = $cookieStore.get('quay.notedplan');
$cookieStore.put('quay.notedplan', '');
var planId = CookieService.get('quay.notedplan');
CookieService.clear('quay.notedplan');
return planId;
};
@ -1842,7 +1863,7 @@ quayApp.directive('namespaceSelector', function () {
'namespace': '=namespace',
'requireCreate': '=requireCreate'
},
controller: function($scope, $element, $routeParams, $cookies) {
controller: function($scope, $element, $routeParams, CookieService) {
$scope.namespaces = {};
$scope.initialize = function(user) {
@ -1854,7 +1875,7 @@ quayApp.directive('namespaceSelector', function () {
}
}
var initialNamespace = $routeParams['namespace'] || $cookies.current_namespace|| $scope.user.username;
var initialNamespace = $routeParams['namespace'] || CookieService.get('quay.namespace') || $scope.user.username;
$scope.namespaces = namespaces;
$scope.setNamespace($scope.namespaces[initialNamespace]);
};
@ -1871,7 +1892,10 @@ quayApp.directive('namespaceSelector', function () {
var newNamespace = namespaceObj.name || namespaceObj.username;
$scope.namespaceObj = namespaceObj;
$scope.namespace = newNamespace;
$cookies.current_namespace = newNamespace;
if (newNamespace) {
CookieService.putPermanent('quay.namespace', newNamespace);
}
};
$scope.$watch('user', function(user) {
@ -1959,8 +1983,8 @@ quayApp.directive('ngBlur', function() {
};
});
quayApp.run(['$location', '$rootScope', 'Restangular', 'UserService', 'PlanService', '$http', '$cookieStore', '$timeout',
function($location, $rootScope, Restangular, UserService, PlanService, $http, $cookieStore, $timeout) {
quayApp.run(['$location', '$rootScope', 'Restangular', 'UserService', 'PlanService', '$http', '$timeout',
function($location, $rootScope, Restangular, UserService, PlanService, $http, $timeout) {
// Handle session expiration.
Restangular.setErrorInterceptor(function(response) {
if (response.status == 401) {