Refactor Glidera Authorization process
This commit is contained in:
parent
fc52a8cf87
commit
fcae682954
6 changed files with 230 additions and 229 deletions
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('glideraService', function($http, $log, $window, platformInfo, storageService, buyAndSellService) {
|
||||
angular.module('copayApp.services').factory('glideraService', function($http, $log, $window, platformInfo, storageService, buyAndSellService, lodash) {
|
||||
var root = {};
|
||||
var credentials = {};
|
||||
var isCordova = platformInfo.isCordova;
|
||||
|
|
@ -45,7 +45,7 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l
|
|||
};
|
||||
};
|
||||
|
||||
root.getEnvironment = function() {
|
||||
root.getNetwork = function() {
|
||||
return credentials.NETWORK;
|
||||
};
|
||||
|
||||
|
|
@ -61,10 +61,14 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l
|
|||
return credentials.HOST + '/oauth2/auth?response_type=code&client_id=' + credentials.CLIENT_ID + '&redirect_uri=' + credentials.REDIRECT_URI;
|
||||
};
|
||||
|
||||
root.removeToken = function(cb) {
|
||||
root.remove = function(cb) {
|
||||
storageService.removeGlideraToken(credentials.NETWORK, function() {
|
||||
buyAndSellService.updateLink('glidera', false);
|
||||
return cb();
|
||||
storageService.removeGlideraPermissions(credentials.NETWORK, function() {
|
||||
storageService.removeGlideraStatus(credentials.NETWORK, function() {
|
||||
buyAndSellService.updateLink('glidera', false);
|
||||
return cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -87,8 +91,6 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l
|
|||
|
||||
$http(req).then(function(data) {
|
||||
$log.info('Glidera Authorization Access Token: SUCCESS');
|
||||
// Show pending task from the UI
|
||||
storageService.setNextStep('BuyAndSell', 'true', function(err) {});
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('Glidera Authorization Access Token: ERROR ' + data.statusText);
|
||||
|
|
@ -96,6 +98,31 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l
|
|||
});
|
||||
};
|
||||
|
||||
root.authorize = function(code, cb) {
|
||||
root.getToken(code, function(err, data) {
|
||||
if (err) return cb(err);
|
||||
if (data && !data.access_token) return cb('No access token');
|
||||
var accessToken = data.access_token;
|
||||
root.getAccessTokenPermissions(accessToken, function(err, p) {
|
||||
if (err) return cb(err);
|
||||
root.getStatus(accessToken, function(err, status) {
|
||||
if (err) $log.error(err);
|
||||
storageService.setGlideraToken(credentials.NETWORK, accessToken, function() {
|
||||
storageService.setGlideraPermissions(credentials.NETWORK, JSON.stringify(p), function() {
|
||||
storageService.setGlideraStatus(credentials.NETWORK, JSON.stringify(status), function() {
|
||||
return cb(null, {
|
||||
token: accessToken,
|
||||
permissions: p,
|
||||
status: status
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var _get = function(endpoint, token) {
|
||||
return {
|
||||
method: 'GET',
|
||||
|
|
@ -289,40 +316,65 @@ angular.module('copayApp.services').factory('glideraService', function($http, $l
|
|||
});
|
||||
};
|
||||
|
||||
root.init = function(accessToken, cb) {
|
||||
$log.debug('Init Glidera...');
|
||||
|
||||
var glidera = {
|
||||
token: null,
|
||||
permissions: null
|
||||
root.init = function(cb) {
|
||||
if (lodash.isEmpty(credentials.CLIENT_ID)) {
|
||||
return cb('Glidera is Disabled');
|
||||
}
|
||||
$log.debug('Trying to initialise Glidera...');
|
||||
|
||||
var getToken = function(cb) {
|
||||
if (accessToken) {
|
||||
cb(null, accessToken);
|
||||
} else {
|
||||
storageService.getGlideraToken(credentials.NETWORK, cb);
|
||||
}
|
||||
};
|
||||
|
||||
getToken(function(err, accessToken) {
|
||||
if (err || !accessToken) return cb();
|
||||
else {
|
||||
buyAndSellService.updateLink('glidera', true);
|
||||
|
||||
root.getAccessTokenPermissions(accessToken, function(err, p) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
} else {
|
||||
glidera.token = accessToken;
|
||||
glidera.permissions = p;
|
||||
return cb(null, glidera);
|
||||
}
|
||||
storageService.getGlideraToken(credentials.NETWORK, function(err, accessToken) {
|
||||
if (err || lodash.isEmpty(accessToken)) return cb();
|
||||
|
||||
storageService.getGlideraPermissions(credentials.NETWORK, function(err, permissions) {
|
||||
if (lodash.isString(permissions)) permissions = JSON.parse(permissions);
|
||||
storageService.getGlideraStatus(credentials.NETWORK, function(err, status) {
|
||||
if (lodash.isString(status)) status = JSON.parse(status);
|
||||
buyAndSellService.updateLink('glidera', true);
|
||||
return cb(null, {
|
||||
token: accessToken,
|
||||
permissions: permissions,
|
||||
status: status
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.updateStatus = function(data) {
|
||||
storageService.getGlideraToken(credentials.NETWORK, function(err, accessToken) {
|
||||
if (err) return;
|
||||
root.getAccessTokenPermissions(accessToken, function(err, permissions) {
|
||||
if (err) return;
|
||||
storageService.setGlideraPermissions(credentials.NETWORK, JSON.stringify(permissions), function() {});
|
||||
data.permissions = permissions;
|
||||
root.getStatus(accessToken, function(err, status) {
|
||||
data.status = status;
|
||||
storageService.setGlideraStatus(credentials.NETWORK, JSON.stringify(status), function() {});
|
||||
|
||||
root.getLimits(accessToken, function(err, limits) {
|
||||
data.limits = limits;
|
||||
|
||||
if (permissions.transaction_history) {
|
||||
root.getTransactions(accessToken, function(err, txs) {
|
||||
data.txs = txs;
|
||||
});
|
||||
}
|
||||
|
||||
if (permissions.view_email_address) {
|
||||
root.getEmail(accessToken, function(err, email) {
|
||||
data.email = email;
|
||||
});
|
||||
}
|
||||
if (permissions.personal_info) {
|
||||
root.getPersonalInfo(accessToken, function(err, info) {
|
||||
data.personalInfo = info;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var register = function() {
|
||||
if (isWindowsPhoneApp) return;
|
||||
|
|
|
|||
|
|
@ -247,6 +247,30 @@ angular.module('copayApp.services')
|
|||
storage.remove('glideraToken-' + network, cb);
|
||||
};
|
||||
|
||||
root.setGlideraPermissions = function(network, p, cb) {
|
||||
storage.set('glideraPermissions-' + network, p, cb);
|
||||
};
|
||||
|
||||
root.getGlideraPermissions = function(network, cb) {
|
||||
storage.get('glideraPermissions-' + network, cb);
|
||||
};
|
||||
|
||||
root.removeGlideraPermissions = function(network, cb) {
|
||||
storage.remove('glideraPermissions-' + network, cb);
|
||||
};
|
||||
|
||||
root.setGlideraStatus = function(network, status, cb) {
|
||||
storage.set('glideraStatus-' + network, status, cb);
|
||||
};
|
||||
|
||||
root.getGlideraStatus = function(network, cb) {
|
||||
storage.get('glideraStatus-' + network, cb);
|
||||
};
|
||||
|
||||
root.removeGlideraStatus = function(network, cb) {
|
||||
storage.remove('glideraStatus-' + network, cb);
|
||||
};
|
||||
|
||||
root.setCoinbaseRefreshToken = function(network, token, cb) {
|
||||
storage.set('coinbaseRefreshToken-' + network, token, cb);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue