Skip to content

Commit

Permalink
override satellizer storage to allow multiple authentications with sa…
Browse files Browse the repository at this point in the history
…tellizer
  • Loading branch information
nicgirault committed Dec 10, 2015
1 parent d18d4b9 commit f83bcec
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bower_components
node_modules
npm-debug.log
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "angular-trello-api-client",
"main": "dist/angular-trello-api-client.js",
"homepage": "https://github.com/nicgirault/angular-trello-api-client",
"version": "1.0.5",
"version": "2.0.0",
"authors": [
"nicgirault <nic.girault@gmail.com>"
],
Expand Down
4 changes: 2 additions & 2 deletions demo/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('demo', [
'ng',
'angular-trello-api-client',
'trello-api-client',
'satellizer'
])

Expand All @@ -22,7 +22,7 @@ angular.module('demo', [
TrelloClient.get('/members/me/boards').then(function(response){
console.log(response);
}).catch(function(error){
console.log(error);
console.warn(error);
});
};
});
2 changes: 1 addition & 1 deletion dist/angular-trello-api-client-min.js

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

28 changes: 16 additions & 12 deletions dist/angular-trello-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ angular.module('trello-api-client').constant('TrelloClientConfig', {
version: 1,
tokenExpiration: 'never',
scope: ['read', 'write', 'account'],
localStoragePrefix: 'trello'
localStorageTokenName: 'trello_token'
});

angular.module('trello-api-client').factory('TrelloInterceptor', [
'$q', 'SatellizerConfig', 'SatellizerStorage', 'SatellizerShared', 'TrelloClientConfig', function($q, config, storage, shared, TrelloClientConfig) {
'$q', 'SatellizerShared', 'TrelloClientConfig', function($q, shared, TrelloClientConfig) {
return {
request: function(request) {
var token, tokenName;
var token;
if (!request.trelloRequest) {
return request;
}
if (shared.isAuthenticated()) {
tokenName = config.tokenPrefix ? config.tokenPrefix + '_' + config.tokenName : config.tokenName;
token = storage.get(tokenName);
token = localStorage.getItem(TrelloClientConfig.localStorageTokenName);
if (token != null) {
if (request.params == null) {
request.params = {};
}
Expand All @@ -48,7 +47,6 @@ angular.module('trello-api-client').provider('TrelloClient', function($authProvi
return;
}
angular.extend(TrelloClientConfig, config);
$authProvider.tokenPrefix = TrelloClientConfig.localStoragePrefix;
$authProvider.httpInterceptor = function(request) {
return false;
};
Expand All @@ -68,27 +66,33 @@ angular.module('trello-api-client').provider('TrelloClient', function($authProvi
expiration: TrelloClientConfig.tokenExpiration
});
};
this.$get = function($location, $http, $window, $auth) {
this.$get = function($location, $http, $window, $auth, $q) {
var TrelloClient, baseURL, fn, i, len, method, ref;
baseURL = TrelloClientConfig.apiEndpoint + "/" + TrelloClientConfig.version;
TrelloClient = {};
TrelloClient.authenticate = function() {
return $auth.authenticate(TrelloClientConfig.appName).then(function(response) {
$auth.setToken(response.token);
localStorage.setItem(TrelloClientConfig.localStorageTokenName, response.token);
return response;
});
};
ref = ['get', 'post', 'put', 'delete'];
fn = function(method) {
return TrelloClient[method] = function(endpoint, config) {
var deferred;
if (config == null) {
config = {};
}
config.trelloRequest = true;
if (!$auth.isAuthenticated()) {
return;
deferred = $q.defer();
if (localStorage.getItem(TrelloClientConfig.localStorageTokenName) == null) {
deferred.reject('Not authenticated');
} else {
$http[method](baseURL + endpoint, config).then(function(response) {
return deferred.resolve(response);
});
}
return $http[method](baseURL + endpoint, config);
return deferred.promise;
};
};
for (i = 0, len = ref.length; i < len; i++) {
Expand Down
9 changes: 1 addition & 8 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ gulp.task('build', function() {
])
.pipe(coffee({bare: true}))
.pipe(concat('angular-trello-api-client.js'))
.pipe(minify())
.pipe(gulp.dest('./dist'))
});

gulp.task('watch', ['build'], function () {
gulp.watch('./src/*.coffee', ['build']);
});

gulp.task('release', ['build'], function() {
gulp.src('dist/*.js')
.pipe(minify({
ignoreFiles: ['.min.js']
}))
.pipe(gulp.dest('dist'))
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-trello-api-client",
"version": "1.0.5",
"version": "2.0.0",
"description": "",
"main": "gulpfile.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ angular.module 'trello-api-client'
version: 1
tokenExpiration: 'never'
scope: ['read', 'write', 'account']
localStoragePrefix: 'trello'
localStorageTokenName: 'trello_token'
}
9 changes: 3 additions & 6 deletions src/interceptor.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
angular.module 'trello-api-client'
.factory 'TrelloInterceptor', [
'$q'
'SatellizerConfig'
'SatellizerStorage'
'SatellizerShared'
'TrelloClientConfig'
($q, config, storage, shared, TrelloClientConfig) ->
($q, shared, TrelloClientConfig) ->
request: (request) ->
return request unless request.trelloRequest

if shared.isAuthenticated()
tokenName = if config.tokenPrefix then config.tokenPrefix + '_' + config.tokenName else config.tokenName
token = storage.get tokenName
token = localStorage.getItem TrelloClientConfig.localStorageTokenName
if token?
request.params ?= {}
request.params.key = TrelloClientConfig.key
request.params.token = token
Expand Down
17 changes: 12 additions & 5 deletions src/provider.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ angular.module 'trello-api-client'
return unless config?
angular.extend TrelloClientConfig, config

$authProvider.tokenPrefix = TrelloClientConfig.localStoragePrefix
$authProvider.httpInterceptor = (request) -> false
$authProvider.oauth2 {
name: TrelloClientConfig.appName
Expand All @@ -22,20 +21,28 @@ angular.module 'trello-api-client'
expiration: TrelloClientConfig.tokenExpiration
}

@$get = ($location, $http, $window, $auth) ->
@$get = ($location, $http, $window, $auth, $q) ->
baseURL = "#{ TrelloClientConfig.apiEndpoint }/#{ TrelloClientConfig.version }"
TrelloClient = {}
TrelloClient.authenticate = ->
$auth.authenticate(TrelloClientConfig.appName).then (response)->
$auth.setToken response.token
localStorage.setItem TrelloClientConfig.localStorageTokenName, response.token
return response
for method in ['get', 'post', 'put', 'delete']
do (method) ->
TrelloClient[method] = (endpoint, config) ->
config ?= {}
config.trelloRequest = true # for interceptor
return unless $auth.isAuthenticated()
$http[method] baseURL + endpoint, config
deferred = $q.defer()
# unless localStorage.getItem(TrelloClientConfig.localStorageTokenName)?
if not localStorage.getItem(TrelloClientConfig.localStorageTokenName)?
deferred.reject 'Not authenticated'
else
$http[method] baseURL + endpoint, config
.then (response) ->
deferred.resolve response

deferred.promise

return TrelloClient
return

0 comments on commit f83bcec

Please sign in to comment.