Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding config to choose storage #129

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 58 additions & 30 deletions dist/angular-oauth2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* angular-oauth2 - Angular OAuth2
* @version v4.1.1
* @version v4.2.0
* @link https://github.com/seegno/angular-oauth2
* @license MIT
*/
Expand All @@ -18,6 +18,31 @@
$httpProvider.interceptors.push("oauthInterceptor");
}
oauthConfig.$inject = [ "$httpProvider" ];
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function request(config) {
config.headers = config.headers || {};
if (!config.headers.hasOwnProperty("Authorization") && OAuthToken.getAuthorizationHeader()) {
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
}
return config;
},
responseError: function responseError(rejection) {
if (!rejection) {
return $q.reject(rejection);
}
if (400 === rejection.status && rejection.data && ("invalid_request" === rejection.data.error || "invalid_grant" === rejection.data.error)) {
OAuthToken.removeToken();
$rootScope.$emit("oauth:error", rejection);
}
if (401 === rejection.status && rejection.data && "invalid_token" === rejection.data.error || rejection.headers && rejection.headers("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) {
$rootScope.$emit("oauth:error", rejection);
}
return $q.reject(rejection);
}
};
}
oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ];
var _createClass = function() {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
Expand Down Expand Up @@ -189,7 +214,8 @@
var config = {
name: "token",
options: {
secure: true
secure: true,
storage: "cookie"
}
};
this.configure = function(params) {
Expand All @@ -207,12 +233,30 @@
_createClass(OAuthToken, [ {
key: "setToken",
value: function setToken(data) {
return $cookies.putObject(config.name, data, config.options);
switch (config.options.storage) {
case "localStorage":
return window.localStorage.setItem(config.name, JSON.stringify(data));

case "sessionStorage":
return window.sessionStorage.setItem(config.name, JSON.stringify(data));

default:
return $cookies.putObject(config.name, data, config.options);
}
}
}, {
key: "getToken",
value: function getToken() {
return $cookies.getObject(config.name);
switch (config.options.storage) {
case "localStorage":
return JSON.parse(window.localStorage.getItem(config.name));

case "sessionStorage":
return JSON.parse(window.sessionStorage.getItem(config.name));

default:
return $cookies.getObject(config.name);
}
}
}, {
key: "getAccessToken",
Expand Down Expand Up @@ -248,7 +292,16 @@
}, {
key: "removeToken",
value: function removeToken() {
return $cookies.remove(config.name, config.options);
switch (config.options.storage) {
case "localStorage":
return window.localStorage.removeItem(config.name);

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

default:
return $cookies.remove(config.name, config.options);
}
}
} ]);
return OAuthToken;
Expand All @@ -257,30 +310,5 @@
};
this.$get.$inject = [ "$cookies" ];
}
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function request(config) {
config.headers = config.headers || {};
if (!config.headers.hasOwnProperty("Authorization") && OAuthToken.getAuthorizationHeader()) {
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
}
return config;
},
responseError: function responseError(rejection) {
if (!rejection) {
return $q.reject(rejection);
}
if (400 === rejection.status && rejection.data && ("invalid_request" === rejection.data.error || "invalid_grant" === rejection.data.error)) {
OAuthToken.removeToken();
$rootScope.$emit("oauth:error", rejection);
}
if (401 === rejection.status && rejection.data && "invalid_token" === rejection.data.error || rejection.headers && rejection.headers("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) {
$rootScope.$emit("oauth:error", rejection);
}
return $q.reject(rejection);
}
};
}
oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ];
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.

30 changes: 26 additions & 4 deletions src/providers/oauth-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function OAuthTokenProvider() {
var config = {
name: 'token',
options: {
secure: true
secure: true,
storage: 'cookie'
}
};

Expand Down Expand Up @@ -47,15 +48,29 @@ function OAuthTokenProvider() {
*/

setToken(data) {
return $cookies.putObject(config.name, data, config.options);
switch (config.options.storage){
case 'localStorage':
return window.localStorage.setItem(config.name, JSON.stringify(data));
case 'sessionStorage':
return window.sessionStorage.setItem(config.name, JSON.stringify(data));
default:
return $cookies.putObject(config.name, data, config.options);
}
}

/**
* Get token.
*/

getToken() {
return $cookies.getObject(config.name);
switch (config.options.storage){
case 'localStorage':
return window.localStorage.getItem(config.name);
case 'sessionStorage':
return window.sessionStorage.getItem(config.name);
default:
return $cookies.getObject(config.name);
}
}

/**
Expand Down Expand Up @@ -108,7 +123,14 @@ function OAuthTokenProvider() {
*/

removeToken() {
return $cookies.remove(config.name, config.options);
switch (config.options.storage){
case 'localStorage':
return window.localStorage.removeItem(config.name);
case 'sessionStorage':
return window.sessionStorage.removeItem(config.name);
default:
return $cookies.remove(config.name, config.options);
}
}
}

Expand Down