Skip to content

Commit

Permalink
fixed the order of .config block so that provider is registered befor…
Browse files Browse the repository at this point in the history
…e it can be used

Fixes #200
  • Loading branch information
sahat committed Nov 26, 2014
1 parent eb8413d commit 3cff252
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 202 deletions.
186 changes: 96 additions & 90 deletions dist/satellizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@
* License: MIT
*/

angular.module('satellizer', [])
.config(['$httpProvider', '$authProvider', 'satellizer.config', function($httpProvider, $authProvider, config) {
$httpProvider.interceptors.push(['$q', function($q) {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return {
request: function(httpConfig) {
var token = localStorage.getItem(tokenName);
if (token) {
token = config.authHeader === 'Authorization' ? 'Bearer ' + token : token;
httpConfig.headers[config.authHeader] = token;
}
return httpConfig;
},
responseError: function(response) {
return $q.reject(response);
}
};
}]);
}]);
angular.module('satellizer', []);

angular.module('satellizer')
.constant('satellizer.config', {
Expand Down Expand Up @@ -111,77 +93,6 @@ angular.module('satellizer')
}
});

angular.module('satellizer')
.factory('satellizer.shared', [
'$q',
'$window',
'$location',
'satellizer.config',
function($q, $window, $location, config) {
var shared = {};

shared.getToken = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return $window.localStorage[tokenName];
};

shared.getPayload = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token && token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse($window.atob(base64));
}
};

shared.setToken = function(response, isLinking) {
var token = response.access_token || response.data[config.tokenName];
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;

if (!token) {
throw new Error('Expecting a token named "' + config.tokenName + '" but instead got: ' + JSON.stringify(response.data));
}

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
$location.path(config.loginRedirect);
}
};

shared.isAuthenticated = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token) {
if (token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
var exp = JSON.parse($window.atob(base64)).exp;
return Math.round(new Date().getTime() / 1000) <= exp;
} else {
return true;
}
}

return false;
};

shared.logout = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
delete $window.localStorage[tokenName];

if (config.logoutRedirect) {
$location.path(config.logoutRedirect);
}
return $q.when();
};

return shared;
}]);

angular.module('satellizer')
.provider('$auth', ['satellizer.config', function(config) {
Object.defineProperties(this, {
Expand Down Expand Up @@ -309,6 +220,77 @@ angular.module('satellizer')

}]);

angular.module('satellizer')
.factory('satellizer.shared', [
'$q',
'$window',
'$location',
'satellizer.config',
function($q, $window, $location, config) {
var shared = {};

shared.getToken = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return $window.localStorage[tokenName];
};

shared.getPayload = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token && token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse($window.atob(base64));
}
};

shared.setToken = function(response, isLinking) {
var token = response.access_token || response.data[config.tokenName];
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;

if (!token) {
throw new Error('Expecting a token named "' + config.tokenName + '" but instead got: ' + JSON.stringify(response.data));
}

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
$location.path(config.loginRedirect);
}
};

shared.isAuthenticated = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token) {
if (token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
var exp = JSON.parse($window.atob(base64)).exp;
return Math.round(new Date().getTime() / 1000) <= exp;
} else {
return true;
}
}

return false;
};

shared.logout = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
delete $window.localStorage[tokenName];

if (config.logoutRedirect) {
$location.path(config.logoutRedirect);
}
return $q.when();
};

return shared;
}]);

angular.module('satellizer')
.factory('satellizer.popup', [
'$q',
Expand Down Expand Up @@ -600,6 +582,30 @@ angular.module('satellizer')
};
});

angular.module('satellizer')
.config([
'$httpProvider',
'$authProvider',
'satellizer.config',
function($httpProvider, $authProvider, config) {
$httpProvider.interceptors.push(['$q', function($q) {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return {
request: function(httpConfig) {
var token = localStorage.getItem(tokenName);
if (token) {
token = config.authHeader === 'Authorization' ? 'Bearer ' + token : token;
httpConfig.headers[config.authHeader] = token;
}
return httpConfig;
},
responseError: function(response) {
return $q.reject(response);
}
};
}]);
}]);

// Base64.js Polyfill (@davidchambers)
(function() {
var object = typeof exports != 'undefined' ? exports : this;
Expand Down
2 changes: 1 addition & 1 deletion dist/satellizer.min.js

Large diffs are not rendered by default.

162 changes: 72 additions & 90 deletions examples/client/vendor/satellizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@
* License: MIT
*/

angular.module('satellizer', [])
.config(['$httpProvider', '$authProvider', 'satellizer.config', function($httpProvider, $authProvider, config) {
$httpProvider.interceptors.push(['$q', function($q) {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return {
request: function(httpConfig) {
var token = localStorage.getItem(tokenName);
if (token) {
token = config.authHeader === 'Authorization' ? 'Bearer ' + token : token;
httpConfig.headers[config.authHeader] = token;
}
return httpConfig;
},
responseError: function(response) {
return $q.reject(response);
}
};
}]);
}]);
angular.module('satellizer', []);

angular.module('satellizer')
.constant('satellizer.config', {
Expand Down Expand Up @@ -111,77 +93,6 @@ angular.module('satellizer')
}
});

angular.module('satellizer')
.factory('satellizer.shared', [
'$q',
'$window',
'$location',
'satellizer.config',
function($q, $window, $location, config) {
var shared = {};

shared.getToken = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return $window.localStorage[tokenName];
};

shared.getPayload = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token && token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse($window.atob(base64));
}
};

shared.setToken = function(response, isLinking) {
var token = response.access_token || response.data[config.tokenName];
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;

if (!token) {
throw new Error('Expecting a token named "' + config.tokenName + '" but instead got: ' + JSON.stringify(response.data));
}

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
$location.path(config.loginRedirect);
}
};

shared.isAuthenticated = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token) {
if (token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
var exp = JSON.parse($window.atob(base64)).exp;
return Math.round(new Date().getTime() / 1000) <= exp;
} else {
return true;
}
}

return false;
};

shared.logout = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
delete $window.localStorage[tokenName];

if (config.logoutRedirect) {
$location.path(config.logoutRedirect);
}
return $q.when();
};

return shared;
}]);

angular.module('satellizer')
.provider('$auth', ['satellizer.config', function(config) {
Object.defineProperties(this, {
Expand Down Expand Up @@ -309,6 +220,77 @@ angular.module('satellizer')

}]);

angular.module('satellizer')
.factory('satellizer.shared', [
'$q',
'$window',
'$location',
'satellizer.config',
function($q, $window, $location, config) {
var shared = {};

shared.getToken = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
return $window.localStorage[tokenName];
};

shared.getPayload = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token && token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
return JSON.parse($window.atob(base64));
}
};

shared.setToken = function(response, isLinking) {
var token = response.access_token || response.data[config.tokenName];
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;

if (!token) {
throw new Error('Expecting a token named "' + config.tokenName + '" but instead got: ' + JSON.stringify(response.data));
}

$window.localStorage[tokenName] = token;

if (config.loginRedirect && !isLinking) {
$location.path(config.loginRedirect);
}
};

shared.isAuthenticated = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
var token = $window.localStorage[tokenName];

if (token) {
if (token.split('.').length === 3) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace('-', '+').replace('_', '/');
var exp = JSON.parse($window.atob(base64)).exp;
return Math.round(new Date().getTime() / 1000) <= exp;
} else {
return true;
}
}

return false;
};

shared.logout = function() {
var tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
delete $window.localStorage[tokenName];

if (config.logoutRedirect) {
$location.path(config.logoutRedirect);
}
return $q.when();
};

return shared;
}]);

angular.module('satellizer')
.factory('satellizer.popup', [
'$q',
Expand Down
Loading

0 comments on commit 3cff252

Please sign in to comment.