Refactor Glidera Authorization process
This commit is contained in:
parent
fc52a8cf87
commit
fcae682954
6 changed files with 230 additions and 229 deletions
|
|
@ -1,72 +1,37 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('glideraController',
|
||||
function($scope, $timeout, $ionicModal, $log, storageService, glideraService, ongoingProcess, platformInfo, externalLinkService, popupService, gettextCatalog) {
|
||||
|
||||
$scope.network = glideraService.getEnvironment();
|
||||
function($scope, $timeout, $ionicModal, $log, storageService, glideraService, ongoingProcess, platformInfo, externalLinkService, popupService, lodash) {
|
||||
|
||||
$scope.openExternalLink = function(url) {
|
||||
externalLinkService.open(url);
|
||||
};
|
||||
|
||||
var initGlidera = function(accessToken) {
|
||||
$scope.token = accessToken;
|
||||
$scope.permissions = null;
|
||||
$scope.email = null;
|
||||
$scope.personalInfo = null;
|
||||
$scope.txs = null;
|
||||
$scope.status = null;
|
||||
$scope.limits = null;
|
||||
var init = function() {
|
||||
glideraService.init(function(err, data) {
|
||||
if (err || lodash.isEmpty(data)) return;
|
||||
|
||||
$scope.connectingGlidera = true;
|
||||
ongoingProcess.set('connectingGlidera', true);
|
||||
glideraService.init($scope.token, function(err, glidera) {
|
||||
ongoingProcess.set('connectingGlidera');
|
||||
$scope.connectingGlidera = false;
|
||||
if (err || !glidera) {
|
||||
if (err) popupService.showAlert(gettextCatalog.getString('Error'), err);
|
||||
return;
|
||||
}
|
||||
$scope.token = glidera.token;
|
||||
$scope.permissions = glidera.permissions;
|
||||
$scope.update({
|
||||
fullUpdate: true
|
||||
$scope.account['token'] = data.token;
|
||||
$scope.account['status'] = data.status;
|
||||
$scope.account['price'] = {};
|
||||
|
||||
glideraService.buyPrice($scope.account.token, {qty: 1}, function(err, buy) {
|
||||
$scope.account.price['buy'] = buy.price;
|
||||
});
|
||||
glideraService.sellPrice($scope.account.token, {qty: 1}, function(err, sell) {
|
||||
$scope.account.price['sell'] = sell.price;
|
||||
});
|
||||
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
$scope.update();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.update = function(opts) {
|
||||
if (!$scope.token || !$scope.permissions) return;
|
||||
$log.debug('Updating Glidera Account...');
|
||||
var accessToken = $scope.token;
|
||||
var permissions = $scope.permissions;
|
||||
|
||||
opts = opts || {};
|
||||
|
||||
glideraService.getStatus(accessToken, function(err, data) {
|
||||
$scope.status = data;
|
||||
});
|
||||
|
||||
glideraService.getLimits(accessToken, function(err, limits) {
|
||||
$scope.limits = limits;
|
||||
});
|
||||
|
||||
if (permissions.transaction_history) {
|
||||
glideraService.getTransactions(accessToken, function(err, data) {
|
||||
$scope.txs = data;
|
||||
});
|
||||
}
|
||||
|
||||
if (permissions.view_email_address && opts.fullUpdate) {
|
||||
glideraService.getEmail(accessToken, function(err, data) {
|
||||
$scope.email = data.email;
|
||||
});
|
||||
}
|
||||
if (permissions.personal_info && opts.fullUpdate) {
|
||||
glideraService.getPersonalInfo(accessToken, function(err, data) {
|
||||
$scope.personalInfo = data;
|
||||
});
|
||||
}
|
||||
$log.debug('Updating Glidera...');
|
||||
glideraService.updateStatus($scope.account);
|
||||
};
|
||||
|
||||
$scope.getAuthenticateUrl = function() {
|
||||
|
|
@ -75,29 +40,22 @@ angular.module('copayApp.controllers').controller('glideraController',
|
|||
|
||||
$scope.submitOauthCode = function(code) {
|
||||
ongoingProcess.set('connectingGlidera', true);
|
||||
$timeout(function() {
|
||||
glideraService.getToken(code, function(err, data) {
|
||||
ongoingProcess.set('connectingGlidera', false);
|
||||
if (err) {
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), err);
|
||||
} else if (data && data.access_token) {
|
||||
storageService.setGlideraToken($scope.network, data.access_token, function() {
|
||||
initGlidera(data.access_token);
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
});
|
||||
}, 100);
|
||||
glideraService.authorize(code, function(err, data) {
|
||||
ongoingProcess.set('connectingGlidera', false);
|
||||
if (err) {
|
||||
popupService.showAlert('Authorisation error', err);
|
||||
return;
|
||||
}
|
||||
init();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.openTxModal = function(token, tx) {
|
||||
$scope.openTxModal = function(tx) {
|
||||
$scope.tx = tx;
|
||||
|
||||
glideraService.getTransaction(token, tx.transactionUuid, function(err, tx) {
|
||||
glideraService.getTransaction($scope.account.token, tx.transactionUuid, function(err, tx) {
|
||||
if (err) {
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get transactions'));
|
||||
popupService.showAlert('Error getting transaction', 'Could not get transactions');
|
||||
return;
|
||||
}
|
||||
$scope.tx = tx;
|
||||
|
|
@ -127,10 +85,10 @@ angular.module('copayApp.controllers').controller('glideraController',
|
|||
$scope.openSupportWindow = function() {
|
||||
var url = glideraService.getSupportUrl();
|
||||
var optIn = true;
|
||||
var title = gettextCatalog.getString('Glidera Support');
|
||||
var message = gettextCatalog.getString('You can email glidera at support@glidera.io for direct support, or you can contact Glidera on Twitter.');
|
||||
var okText = gettextCatalog.getString('Tweet @GlideraInc');
|
||||
var cancelText = gettextCatalog.getString('Go Back');
|
||||
var title = 'Glidera Support';
|
||||
var message = 'You can email glidera at support@glidera.io for direct support, or you can contact Glidera on Twitter.';
|
||||
var okText = 'Tweet @GlideraInc';
|
||||
var cancelText = 'Go Back';
|
||||
externalLinkService.open(url, optIn, title, message, okText, cancelText);
|
||||
}
|
||||
|
||||
|
|
@ -147,8 +105,10 @@ angular.module('copayApp.controllers').controller('glideraController',
|
|||
}
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||
$scope.network = glideraService.getNetwork();
|
||||
$scope.showOauthForm = false;
|
||||
initGlidera();
|
||||
$scope.account = {};
|
||||
init();
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,46 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('preferencesGlideraController',
|
||||
function($scope, $log, $timeout, $state, $ionicHistory, ongoingProcess, glideraService, popupService, gettextCatalog) {
|
||||
|
||||
$scope.update = function(opts) {
|
||||
if (!$scope.token || !$scope.permissions) return;
|
||||
$log.debug('Updating Glidera Account...');
|
||||
var accessToken = $scope.token;
|
||||
var permissions = $scope.permissions;
|
||||
|
||||
opts = opts || {};
|
||||
|
||||
glideraService.getStatus(accessToken, function(err, data) {
|
||||
$scope.status = data;
|
||||
});
|
||||
|
||||
glideraService.getLimits(accessToken, function(err, limits) {
|
||||
$scope.limits = limits;
|
||||
});
|
||||
|
||||
if (permissions.transaction_history) {
|
||||
glideraService.getTransactions(accessToken, function(err, data) {
|
||||
$scope.txs = data;
|
||||
});
|
||||
}
|
||||
|
||||
if (permissions.view_email_address && opts.fullUpdate) {
|
||||
glideraService.getEmail(accessToken, function(err, data) {
|
||||
$scope.email = data;
|
||||
});
|
||||
}
|
||||
if (permissions.personal_info && opts.fullUpdate) {
|
||||
glideraService.getPersonalInfo(accessToken, function(err, data) {
|
||||
$scope.personalInfo = data;
|
||||
});
|
||||
}
|
||||
};
|
||||
function($scope, $timeout, $state, $ionicHistory, glideraService, popupService) {
|
||||
|
||||
$scope.revokeToken = function() {
|
||||
popupService.showConfirm('Glidera', 'Are you sure you would like to log out of your Glidera account?', null, null, function(res) {
|
||||
if (res) {
|
||||
glideraService.removeToken(function() {
|
||||
glideraService.remove(function() {
|
||||
$ionicHistory.clearHistory();
|
||||
$timeout(function() {
|
||||
$state.go('tabs.home');
|
||||
|
|
@ -50,21 +16,20 @@ angular.module('copayApp.controllers').controller('preferencesGlideraController'
|
|||
});
|
||||
};
|
||||
|
||||
$scope.$on("$ionicView.enter", function(event, data){
|
||||
$scope.network = glideraService.getEnvironment();
|
||||
$scope.$on("$ionicView.afterEnter", function(event, data){
|
||||
glideraService.updateStatus($scope.account);
|
||||
});
|
||||
|
||||
ongoingProcess.set('connectingGlidera', true);
|
||||
glideraService.init($scope.token, function(err, glidera) {
|
||||
ongoingProcess.set('connectingGlidera');
|
||||
$scope.$on("$ionicView.beforeEnter", function(event, data){
|
||||
$scope.account = {};
|
||||
glideraService.init(function(err, glidera) {
|
||||
if (err || !glidera) {
|
||||
if (err) popupService.showAlert(gettextCatalog.getString('Error'), err);
|
||||
if (err) popupService.showAlert('Error connecting Glidera', err);
|
||||
return;
|
||||
}
|
||||
$scope.token = glidera.token;
|
||||
$scope.permissions = glidera.permissions;
|
||||
$scope.update({
|
||||
fullUpdate: true
|
||||
});
|
||||
$scope.account['token'] = glidera.token;
|
||||
$scope.account['permissions'] = glidera.permissions;
|
||||
$scope.account['status'] = glidera.status;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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