copay/js/controllers/home.js

217 lines
5.9 KiB
JavaScript
Raw Normal View History

'use strict';
2015-01-28 20:56:32 -08:00
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $timeout, $window, go, notification, identityService, Compatibility, pinService, applicationService, isMobile, isCordova, localstorageService) {
var KEY = 'CopayDisclaimer';
var ls = localstorageService;
2015-01-28 20:56:32 -08:00
var _credentials;
2014-12-02 11:16:31 -08:00
$scope.init = function() {
2014-12-04 15:42:08 -08:00
$scope.isMobile = isMobile.any();
2015-01-03 08:40:35 -08:00
$scope.isWindowsPhoneApp = isMobile.Windows() && isCordova;
2015-01-03 09:26:45 -08:00
$scope.hideForWP = 0;
2015-01-28 20:56:32 -08:00
$scope.attempt = 0;
$scope.digits = [];
$scope.defined = [];
2015-01-28 22:14:32 -08:00
$scope.askForPin = 0;
2014-12-04 15:42:08 -08:00
2014-12-02 11:16:31 -08:00
// This is only for backwards compat, insight api should link to #!/confirmed directly
if (getParam('confirmed')) {
var hashIndex = window.location.href.indexOf('/?');
window.location = window.location.href.substr(0, hashIndex) + '#!/confirmed';
return;
}
2014-12-02 11:16:31 -08:00
if ($rootScope.fromEmailConfirmation) {
$scope.confirmedEmail = true;
$rootScope.fromEmailConfirmation = false;
}
2014-12-02 18:30:44 -08:00
if ($rootScope.wallet) {
go.walletHome();
2014-12-02 18:30:44 -08:00
}
2014-12-02 11:16:31 -08:00
Compatibility.check($scope);
2014-12-03 07:40:13 -08:00
pinService.check(function(err, value) {
$rootScope.hasPin = value;
});
$scope.usingLocalStorage = config.plugins.EncryptedLocalStorage;
if (isCordova) {
ls.getItem(KEY, function(err, value) {
$scope.showDisclaimer = value ? null : true;
});
}
2014-12-02 11:16:31 -08:00
};
2015-01-28 20:56:32 -08:00
$scope.clear = function() {
pinService.clearPin($scope);
};
$scope.press = function(digit) {
pinService.pressPin($scope, digit);
};
$scope.skip = function () {
pinService.skipPin($scope);
};
$scope.agreeDisclaimer = function() {
ls.setItem(KEY, true, function(err) {
$scope.showDisclaimer = null;
});
};
2015-01-03 09:26:45 -08:00
$scope.formFocus = function() {
if ($scope.isWindowsPhoneApp) {
$scope.hideForWP = true;
$timeout(function() {
$scope.$digest();
}, 1);
}
};
2014-12-03 11:01:22 -08:00
$scope.openWithPin = function(pin) {
if (!pin) {
2014-12-02 11:16:31 -08:00
$scope.error = 'Please enter the required fields';
return;
}
$rootScope.starting = true;
2014-12-13 14:38:39 -08:00
2014-12-10 22:41:28 -08:00
$timeout(function() {
var credentials = pinService.get(pin, function(err, credentials) {
if (err || !credentials) {
$rootScope.starting = null;
2014-12-10 22:41:28 -08:00
$scope.error = 'Wrong PIN';
2015-01-28 20:56:32 -08:00
$scope.clear();
$timeout(function() {
$scope.error = null;
}, 2000);
2014-12-10 22:41:28 -08:00
return;
}
$scope.open(credentials.email, credentials.password);
});
2014-12-13 14:38:39 -08:00
}, 100);
2014-12-02 11:16:31 -08:00
};
2014-11-30 18:14:46 -08:00
2014-12-03 07:40:13 -08:00
$scope.openWallets = function() {
preconditions.checkState($rootScope.iden);
var iden = $rootScope.iden;
$rootScope.hideNavigation = false;
$rootScope.starting = true;
iden.openWallets();
};
2014-12-03 08:44:55 -08:00
$scope.createPin = function(pin) {
preconditions.checkArgument(pin);
2014-12-03 07:40:13 -08:00
preconditions.checkState($rootScope.iden);
preconditions.checkState(_credentials && _credentials.email);
$rootScope.starting = true;
$timeout(function() {
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
2015-01-28 22:14:32 -08:00
$scope.askForPin = 0;
$rootScope.hasPin = true;
$rootScope.starting = null;
$scope.openWallets();
});
2014-12-13 14:38:39 -08:00
}, 100);
2014-12-02 11:16:31 -08:00
};
$scope.openWithCredentials = function(form) {
2014-10-09 14:53:31 -07:00
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
2014-10-09 14:53:31 -07:00
return;
}
2014-12-03 07:40:13 -08:00
$timeout(function() {
$scope.open(form.email.$modelValue, form.password.$modelValue);
}, 100);
2014-12-02 11:16:31 -08:00
};
2014-12-02 17:38:33 -08:00
$scope.pinLogout = function() {
pinService.clear(function() {
copay.logger.debug('PIN erased');
2014-12-03 07:40:13 -08:00
delete $rootScope['hasPin'];
2014-12-06 16:07:26 -08:00
applicationService.restart();
2014-12-02 17:38:33 -08:00
});
};
2014-12-02 11:16:31 -08:00
$scope.open = function(email, password) {
2014-11-29 19:31:17 -08:00
$rootScope.starting = true;
2014-12-02 11:16:31 -08:00
identityService.open(email, password, function(err, iden) {
if (err) {
2014-11-30 18:14:46 -08:00
$rootScope.starting = false;
2014-11-29 19:31:17 -08:00
copay.logger.warn(err);
var identifier = $scope.usingLocalStorage ? 'username' : 'email';
2014-11-29 19:31:17 -08:00
if ((err.toString() || '').match('PNOTFOUND')) {
$scope.error = 'Invalid ' + identifier + ' or password';
2014-12-13 14:38:39 -08:00
if ($scope.attempt++ > 1) {
var storage = $scope.usingLocalStorage ? 'this device storage' : 'cloud storage';
$scope.error = 'Invalid ' + identifier + ' or password. You are trying to sign in using ' + storage + '. Change it on settings if necessary.';
};
2014-12-11 16:50:15 -08:00
$rootScope.hasPin = false;
pinService.clear(function() {});
2014-12-02 08:52:17 -08:00
} else if ((err.toString() || '').match('Connection')) {
$scope.error = 'Could not connect to Insight Server';
} else if ((err.toString() || '').match('Unable')) {
$scope.error = 'Unable to read data from the Insight Server';
2014-11-29 19:31:17 -08:00
} else {
$scope.error = 'Unknown error';
}
2014-12-13 14:38:39 -08:00
$timeout(function() {
2014-12-05 08:24:46 -08:00
$rootScope.$digest();
2014-12-13 14:38:39 -08:00
}, 1)
2014-12-03 07:40:13 -08:00
return;
2014-11-29 19:31:17 -08:00
}
2014-11-30 18:14:46 -08:00
2014-12-03 07:40:13 -08:00
// Open successfully?
2014-11-30 18:14:46 -08:00
if (iden) {
$scope.error = null;
2014-12-03 11:01:22 -08:00
$scope.confirmedEmail = false;
2014-12-03 07:40:13 -08:00
// mobile
2014-12-04 15:42:08 -08:00
if ($scope.isMobile && !$rootScope.hasPin) {
2014-12-03 07:40:13 -08:00
_credentials = {
email: email,
password: password,
};
2015-01-28 22:14:32 -08:00
$scope.askForPin = 1;
2014-12-03 07:40:13 -08:00
$rootScope.starting = false;
$rootScope.hideNavigation = true;
2014-12-13 14:38:39 -08:00
$timeout(function() {
$rootScope.$digest();
}, 1);
2014-12-03 07:40:13 -08:00
return;
}
// no mobile
else {
$scope.openWallets();
}
2014-11-30 18:14:46 -08:00
}
2014-11-29 19:31:17 -08:00
});
2014-12-09 09:27:09 -08:00
};
2014-10-30 08:06:48 -07:00
function getParam(sname) {
var params = location.search.substr(location.search.indexOf("?") + 1);
var sval = "";
params = params.split("&");
// split param and value into individual pieces
for (var i = 0; i < params.length; i++) {
var temp = params[i].split("=");
if ([temp[0]] == sname) {
sval = temp[1];
}
}
return sval;
}
2014-09-02 21:25:08 -07:00
});