diff --git a/js/controllers/home.js b/js/controllers/home.js index 1aa4a7a00..eff98ae3f 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -3,7 +3,7 @@ angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility, pinService, applicationService, isMobile) { - var _credentials; + var _credentials, _firstpin; $scope.init = function() { // This is only for backwards compat, insight api should link to #!/confirmed directly @@ -28,62 +28,24 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc }); }; - Object.defineProperty($scope, - "pin", { - get: function() { - return this._pin; - }, - set: function(newValue) { - this._pin = newValue; - if (newValue && newValue.length == 4) { - $scope.openPin(newValue); - } - if (!newValue) { - $scope.error = null; - } - }, - enumerable: true, - configurable: true - }); + pinService.makePinInput($scope, 'pin', function(newValue) { + $scope.openPin(newValue); + }); - Object.defineProperty($scope, - "newpin", { - get: function() { - return this._newpin; - }, - set: function(newValue) { - this._newpin = newValue; - if (newValue && newValue.length == 4) { - // next input - $scope.$$childTail.secondPin = true; - } - }, - enumerable: true, - configurable: true - }); + pinService.makePinInput($scope, 'newpin', function(newValue) { + _firstpin = newValue; + $scope.askForPin = 2; + }); - Object.defineProperty($scope, - "repeatpin", { - get: function() { - return this._repeatpin; - }, - set: function(newValue) { - this._repeatpin = newValue; - if (newValue && newValue.length == 4) { - if ($scope.$$childTail._newpin === newValue) { - // save and submit - $scope.createPin($scope.$$childTail.setPinForm); - } else { - $scope.error = 'Entered PINs were not equal. Try again'; - } - } - if (!newValue) { - $scope.error = null; - } - }, - enumerable: true, - configurable: true - }); + pinService.makePinInput($scope, 'repeatpin', function(newValue) { + if (newValue === _firstpin) { + _firstpin = null; + $scope.createPin(newValue); + } else { + _firstpin = null; + $scope.error = 'Entered PINs were not equal. Try again'; + } + }); $scope.done = function() { $rootScope.starting = false; @@ -131,14 +93,15 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc iden.openWallets(); }; - $scope.createPin = function(form) { - if (!form) return; + $scope.createPin = function(pin) { + preconditions.checkArgument(pin); preconditions.checkState($rootScope.iden); preconditions.checkState(_credentials && _credentials.email); - pinService.save(form.repeatpin.$modelValue, _credentials.email, _credentials.password, function(err) { + pinService.save(pin, _credentials.email, _credentials.password, function(err) { _credentials.password = ''; _credentials = null; + $scope.askForPin = 0; $rootScope.hasPin = true; $scope.openWallets(); }); @@ -197,7 +160,7 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc email: email, password: password, }; - $scope.askForPin = true; + $scope.askForPin = 1; $rootScope.starting = false; $rootScope.hideNavigation = true; $rootScope.$digest(); diff --git a/js/services/pinService.js b/js/services/pinService.js index a104d61fc..c5562029a 100644 --- a/js/services/pinService.js +++ b/js/services/pinService.js @@ -48,5 +48,24 @@ angular.module('copayApp.services') ls.removeItem(KEY, cb); }; + + root.makePinInput = function(scope, name, cb) { + Object.defineProperty(scope, name, { + get: function() { + return this['_' + name]; + }, + set: function(newValue) { + this['_' + name] = newValue; + scope.error = null; + if (newValue && newValue.length == 4) { + return cb(newValue); + } + }, + enumerable: true, + configurable: true + }); + }; + + return root; }); diff --git a/views/home.html b/views/home.html index 30f22b489..ab213bd7a 100644 --- a/views/home.html +++ b/views/home.html @@ -47,7 +47,7 @@ creating your profile -
+

Set up a PIN ?

Enter a 4-digit number for easier access from this device @@ -61,18 +61,18 @@

-
+
+ placeholder="PIN" name="newpin" required show-focus="askForPin == 1">
-
+
+ placeholder="Confirm your PIN" name="repeatpin" required show-focus="askForPin == 2">