Skip to content

Commit

Permalink
add oauthStorageService
Browse files Browse the repository at this point in the history
  • Loading branch information
jarbitlira committed Sep 19, 2015
1 parent 6b92676 commit bd8d16e
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 139 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"dependencies": {
"angular": "^1.3.9",
"angular-cookie": "^4.0.6",
"angular-cookies": "~1.4.6",
"ngstorage": "~0.3.9",
"query-string": "^1.0.0"
}
}
166 changes: 96 additions & 70 deletions dist/angular-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
root.angularOAuth2 = factory(root.angular, root.queryString);
}
})(this, function(angular, queryString) {
var ngModule = angular.module("angular-oauth2", [ "ipCookie" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider);
function oauthConfig($httpProvider) {
$httpProvider.interceptors.push("oauthInterceptor");
}
oauthConfig.$inject = [ "$httpProvider" ];
var ngModule = angular.module("angular-oauth2", [ "ngStorage", "ngCookies" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).factory("OAuthStorageService", OAuthStorageService).provider("OAuthToken", OAuthTokenProvider);
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function(config) {
Expand Down Expand Up @@ -179,7 +175,6 @@
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
function OAuthTokenProvider() {
var storage;
var config = {
name: "token",
storage: "cookies",
Expand All @@ -194,16 +189,18 @@
angular.extend(config, params);
return config;
};
this.$get = function(ipCookie, $window) {
this.$get = function(OAuthStorageService) {
var OAuthToken = function() {
function OAuthToken() {}
function OAuthToken() {
OAuthStorageService.configure(config);
}
_prototypeProperties(OAuthToken, null, {
token: {
set: function(data) {
return setToken(data);
return OAuthStorageService.setToken(data);
},
get: function() {
return getToken();
return OAuthStorageService.getToken();
},
enumerable: true,
configurable: true
Expand Down Expand Up @@ -244,75 +241,104 @@
configurable: true
},
removeToken: {
value: function(_removeToken) {
var _removeTokenWrapper = function removeToken() {
return _removeToken.apply(this, arguments);
};
_removeTokenWrapper.toString = function() {
return _removeToken.toString();
};
return _removeTokenWrapper;
}(function() {
return removeToken();
}),
value: function removeToken() {
return OAuthStorageService.removeToken();
},
writable: true,
enumerable: true,
configurable: true
}
});
return OAuthToken;
}();
var setToken = function(data) {
storage = config.storage.toLowerCase();
switch (storage) {
case "cookies":
return ipCookie(config.name, data, config.options);

case "localstorage":
return $window.localStorage.setItem(config.name, angular.toJson(data));

case "sessionstorage":
return $window.sessionStorage.setItem(config.name, angular.toJson(data));

default:
return ipCookie(config.name, data, config.options);
}
};
var getToken = function() {
storage = config.storage.toLowerCase();
switch (storage) {
case "cookies":
return ipCookie(config.name);

case "localstorage":
return angular.fromJson($window.localStorage.getItem(config.name));

case "sessionstorage":
return angular.fromJson($window.sessionStorage.getItem(config.name));

default:
return ipCookie(config.name);
}
};
var removeToken = function() {
storage = config.storage.toLowerCase();
switch (storage) {
case "cookies":
return ipCookie.remove(config.name, config.options);

case "localstorage":
return $window.localStorage.removeItem(config.name);

case "sessionstorage":
return $window.sessionStorage.removeItem(config.name);

default:
return ipCookie.remove(config.name, config.options);
}
};
return new OAuthToken();
};
this.$get.$inject = [ "ipCookie", "$window" ];
this.$get.$inject = [ "OAuthStorageService" ];
}
function oauthConfig($httpProvider) {
$httpProvider.interceptors.push("oauthInterceptor");
}
oauthConfig.$inject = [ "$httpProvider" ];
var _prototypeProperties = function(child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
function oauthStorageService() {
var config = {};
this.configure = function(params) {
angular.extend(config, params);
return config;
};
this.$get = [ "$localStorage", "$sessionStorage", "$cookies", "$log", function($localStorage, $sessionStorage, $cookies, $log) {
var storage;
var ngStorage = config.storage.toLowerCase();
if (ngStorage === "localstorage") {
storage = $localStorage;
} else if (ngStorage === "sessionstorage") {
storage = $sessionStorage;
} else if (ngStorage === "cookies") {
storage = $cookies;
} else {
storage = $cookies;
$log.warn("Set storage to cookies, because storage type is unknown");
}
var BrowserStorage = function() {
function BrowserStorage(storage, name) {
this.storage = storage;
this.name = name;
}
_prototypeProperties(BrowserStorage, null, {
token: {
set: function(data) {
return this.storage.setItem(this.name, angular.toJson(data));
},
get: function() {
return angular.fromJson(this.storage.getItem(this.name));
},
enumerable: true,
configurable: true
},
deleteToken: {
value: function deleteToken() {
this.storage.removeItem(this.name);
},
writable: true,
enumerable: true,
configurable: true
}
});
return BrowserStorage;
}();
var CookieStorage = function() {
function CookieStorage($cookies, name, options) {
this.$cookies = $cookies;
this.name = name;
this.options = options;
}
_prototypeProperties(CookieStorage, null, {
token: {
set: function(value) {
return this.$cookies.putObject(this.name, value, this.options);
},
get: function() {
return this.$cookies.getObject(this.name);
},
enumerable: true,
configurable: true
},
deleteToken: {
value: function deleteToken() {
return this.$cookies.remove(this.name, this.options);
},
writable: true,
enumerable: true,
configurable: true
}
});
return CookieStorage;
}();
return ngStorage === "cookies" ? new CookieStorage(storage, config.name, config.options) : new BrowserStorage(storage, config.name);
} ];
}
return ngModule;
});
2 changes: 1 addition & 1 deletion dist/angular-oauth2.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"browser": {
"angular": "./bower_components/angular/angular.js",
"ngstorage": "./bower_components/ngstorage/ngStorage.js",
"angular-cookie": "./bower_components/angular-cookie/angular-cookie.js",
"query-string": "./bower_components/query-string/query-string.js"
},
Expand Down
8 changes: 6 additions & 2 deletions src/angular-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import OAuthProvider from './providers/oauth-provider';
import OAuthTokenProvider from './providers/oauth-token-provider';
import oauthConfig from './config/oauth-config';
import oauthInterceptor from './interceptors/oauth-interceptor';
import 'angular-cookie';
import OAuthStorageService from './services/oauth-storage-service';
import 'ngstorage';
import 'angular-cookies';

var ngModule = angular.module('angular-oauth2', [
'ipCookie'
'ngStorage',
'ngCookies'
])
.config(oauthConfig)
.factory('oauthInterceptor', oauthInterceptor)
.provider('OAuth', OAuthProvider)
.provider('OAuthToken', OAuthTokenProvider)
.factory('OAuthStorageService', OAuthStorageService)
;

/**
Expand Down
Loading

0 comments on commit bd8d16e

Please sign in to comment.