Skip to content

Commit bd8d16e

Browse files
committed
add oauthStorageService
1 parent 6b92676 commit bd8d16e

7 files changed

+204
-139
lines changed

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
],
2525
"dependencies": {
2626
"angular": "^1.3.9",
27-
"angular-cookie": "^4.0.6",
27+
"angular-cookies": "~1.4.6",
28+
"ngstorage": "~0.3.9",
2829
"query-string": "^1.0.0"
2930
}
3031
}

dist/angular-oauth2.js

+96-70
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
root.angularOAuth2 = factory(root.angular, root.queryString);
1414
}
1515
})(this, function(angular, queryString) {
16-
var ngModule = angular.module("angular-oauth2", [ "ipCookie" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider);
17-
function oauthConfig($httpProvider) {
18-
$httpProvider.interceptors.push("oauthInterceptor");
19-
}
20-
oauthConfig.$inject = [ "$httpProvider" ];
16+
var ngModule = angular.module("angular-oauth2", [ "ngStorage", "ngCookies" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).factory("OAuthStorageService", OAuthStorageService).provider("OAuthToken", OAuthTokenProvider);
2117
function oauthInterceptor($q, $rootScope, OAuthToken) {
2218
return {
2319
request: function(config) {
@@ -179,7 +175,6 @@
179175
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
180176
};
181177
function OAuthTokenProvider() {
182-
var storage;
183178
var config = {
184179
name: "token",
185180
storage: "cookies",
@@ -194,16 +189,18 @@
194189
angular.extend(config, params);
195190
return config;
196191
};
197-
this.$get = function(ipCookie, $window) {
192+
this.$get = function(OAuthStorageService) {
198193
var OAuthToken = function() {
199-
function OAuthToken() {}
194+
function OAuthToken() {
195+
OAuthStorageService.configure(config);
196+
}
200197
_prototypeProperties(OAuthToken, null, {
201198
token: {
202199
set: function(data) {
203-
return setToken(data);
200+
return OAuthStorageService.setToken(data);
204201
},
205202
get: function() {
206-
return getToken();
203+
return OAuthStorageService.getToken();
207204
},
208205
enumerable: true,
209206
configurable: true
@@ -244,75 +241,104 @@
244241
configurable: true
245242
},
246243
removeToken: {
247-
value: function(_removeToken) {
248-
var _removeTokenWrapper = function removeToken() {
249-
return _removeToken.apply(this, arguments);
250-
};
251-
_removeTokenWrapper.toString = function() {
252-
return _removeToken.toString();
253-
};
254-
return _removeTokenWrapper;
255-
}(function() {
256-
return removeToken();
257-
}),
244+
value: function removeToken() {
245+
return OAuthStorageService.removeToken();
246+
},
258247
writable: true,
259248
enumerable: true,
260249
configurable: true
261250
}
262251
});
263252
return OAuthToken;
264253
}();
265-
var setToken = function(data) {
266-
storage = config.storage.toLowerCase();
267-
switch (storage) {
268-
case "cookies":
269-
return ipCookie(config.name, data, config.options);
270-
271-
case "localstorage":
272-
return $window.localStorage.setItem(config.name, angular.toJson(data));
273-
274-
case "sessionstorage":
275-
return $window.sessionStorage.setItem(config.name, angular.toJson(data));
276-
277-
default:
278-
return ipCookie(config.name, data, config.options);
279-
}
280-
};
281-
var getToken = function() {
282-
storage = config.storage.toLowerCase();
283-
switch (storage) {
284-
case "cookies":
285-
return ipCookie(config.name);
286-
287-
case "localstorage":
288-
return angular.fromJson($window.localStorage.getItem(config.name));
289-
290-
case "sessionstorage":
291-
return angular.fromJson($window.sessionStorage.getItem(config.name));
292-
293-
default:
294-
return ipCookie(config.name);
295-
}
296-
};
297-
var removeToken = function() {
298-
storage = config.storage.toLowerCase();
299-
switch (storage) {
300-
case "cookies":
301-
return ipCookie.remove(config.name, config.options);
302-
303-
case "localstorage":
304-
return $window.localStorage.removeItem(config.name);
305-
306-
case "sessionstorage":
307-
return $window.sessionStorage.removeItem(config.name);
308-
309-
default:
310-
return ipCookie.remove(config.name, config.options);
311-
}
312-
};
313254
return new OAuthToken();
314255
};
315-
this.$get.$inject = [ "ipCookie", "$window" ];
256+
this.$get.$inject = [ "OAuthStorageService" ];
257+
}
258+
function oauthConfig($httpProvider) {
259+
$httpProvider.interceptors.push("oauthInterceptor");
260+
}
261+
oauthConfig.$inject = [ "$httpProvider" ];
262+
var _prototypeProperties = function(child, staticProps, instanceProps) {
263+
if (staticProps) Object.defineProperties(child, staticProps);
264+
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
265+
};
266+
function oauthStorageService() {
267+
var config = {};
268+
this.configure = function(params) {
269+
angular.extend(config, params);
270+
return config;
271+
};
272+
this.$get = [ "$localStorage", "$sessionStorage", "$cookies", "$log", function($localStorage, $sessionStorage, $cookies, $log) {
273+
var storage;
274+
var ngStorage = config.storage.toLowerCase();
275+
if (ngStorage === "localstorage") {
276+
storage = $localStorage;
277+
} else if (ngStorage === "sessionstorage") {
278+
storage = $sessionStorage;
279+
} else if (ngStorage === "cookies") {
280+
storage = $cookies;
281+
} else {
282+
storage = $cookies;
283+
$log.warn("Set storage to cookies, because storage type is unknown");
284+
}
285+
var BrowserStorage = function() {
286+
function BrowserStorage(storage, name) {
287+
this.storage = storage;
288+
this.name = name;
289+
}
290+
_prototypeProperties(BrowserStorage, null, {
291+
token: {
292+
set: function(data) {
293+
return this.storage.setItem(this.name, angular.toJson(data));
294+
},
295+
get: function() {
296+
return angular.fromJson(this.storage.getItem(this.name));
297+
},
298+
enumerable: true,
299+
configurable: true
300+
},
301+
deleteToken: {
302+
value: function deleteToken() {
303+
this.storage.removeItem(this.name);
304+
},
305+
writable: true,
306+
enumerable: true,
307+
configurable: true
308+
}
309+
});
310+
return BrowserStorage;
311+
}();
312+
var CookieStorage = function() {
313+
function CookieStorage($cookies, name, options) {
314+
this.$cookies = $cookies;
315+
this.name = name;
316+
this.options = options;
317+
}
318+
_prototypeProperties(CookieStorage, null, {
319+
token: {
320+
set: function(value) {
321+
return this.$cookies.putObject(this.name, value, this.options);
322+
},
323+
get: function() {
324+
return this.$cookies.getObject(this.name);
325+
},
326+
enumerable: true,
327+
configurable: true
328+
},
329+
deleteToken: {
330+
value: function deleteToken() {
331+
return this.$cookies.remove(this.name, this.options);
332+
},
333+
writable: true,
334+
enumerable: true,
335+
configurable: true
336+
}
337+
});
338+
return CookieStorage;
339+
}();
340+
return ngStorage === "cookies" ? new CookieStorage(storage, config.name, config.options) : new BrowserStorage(storage, config.name);
341+
} ];
316342
}
317343
return ngModule;
318344
});

dist/angular-oauth2.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
},
4848
"browser": {
4949
"angular": "./bower_components/angular/angular.js",
50+
"ngstorage": "./bower_components/ngstorage/ngStorage.js",
5051
"angular-cookie": "./bower_components/angular-cookie/angular-cookie.js",
5152
"query-string": "./bower_components/query-string/query-string.js"
5253
},

src/angular-oauth2.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ import OAuthProvider from './providers/oauth-provider';
88
import OAuthTokenProvider from './providers/oauth-token-provider';
99
import oauthConfig from './config/oauth-config';
1010
import oauthInterceptor from './interceptors/oauth-interceptor';
11-
import 'angular-cookie';
11+
import OAuthStorageService from './services/oauth-storage-service';
12+
import 'ngstorage';
13+
import 'angular-cookies';
1214

1315
var ngModule = angular.module('angular-oauth2', [
14-
'ipCookie'
16+
'ngStorage',
17+
'ngCookies'
1518
])
1619
.config(oauthConfig)
1720
.factory('oauthInterceptor', oauthInterceptor)
1821
.provider('OAuth', OAuthProvider)
1922
.provider('OAuthToken', OAuthTokenProvider)
23+
.factory('OAuthStorageService', OAuthStorageService)
2024
;
2125

2226
/**

0 commit comments

Comments
 (0)