diff --git a/js/controllers/home.js b/js/controllers/home.js index df4c43ab5..c459cb545 100644 --- a/js/controllers/home.js +++ b/js/controllers/home.js @@ -1,28 +1,52 @@ 'use strict'; -angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility) { - // 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; - } +angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility, pinService) { + $scope.init = function() { + // 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; + } - if ($rootScope.fromEmailConfirmation) { - $scope.confirmedEmail = true; - $rootScope.fromEmailConfirmation = false; - } + if ($rootScope.fromEmailConfirmation) { + $scope.confirmedEmail = true; + $rootScope.fromEmailConfirmation = false; + } + Compatibility.check($scope); + $scope.hasPin = pinService.check(); + }; + + Object.defineProperty($scope, + "pin", { + get: function() { + return this._pin; + }, + set: function(newValue) { + console.log('[home.js:26]',newValue, this._pin); //TODO + this._pin = newValue; + if (newValue && newValue.length == 4) { + console.log('[home.js:26] INGRESANDO AUTOMATICAMENTE',newValue); //TODO + $scope.openPin(newValue); + } + if (!newValue) { + $scope.error = null; + } + }, + enumerable: true, + configurable: true + }); - Compatibility.check($scope); $scope.done = function() { + $scope.hasPin = pinService.check(); $rootScope.starting = false; $rootScope.$digest(); }; - $scope.$on("$destroy", function(){ + $scope.$on("$destroy", function() { var iden = $rootScope.iden; if (iden) { iden.removeListener('newWallet', $scope.done ); @@ -30,20 +54,57 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc } }); - - $scope.openProfile = function(form) { + $scope.openWithPin = function(form) { $scope.confirmedEmail = false; if (form && form.$invalid) { $scope.error = 'Please enter the required fields'; return; } + $scope.openPin(pin); + }; + + $scope.openPin = function(pin) { + var credentials = pinService.get(parseInt(pin)); + if (!credentials) { + $scope.error = 'Wrong PIN'; + return; + } + $rootScope.starting = true; - identityService.open(form.email.$modelValue, form.password.$modelValue, function(err, iden) { - if (err) { + $scope.open(credentials.email, credentials.password); + }; + + $scope.createPin = function(form) { + if (form) { + pinService.save(form.repeatpin.$modelValue, $scope.email, $scope.password); + } + $scope.open($scope.email, $scope.password); + }; + + $scope.openWithCredentials = function(form) { + $scope.confirmedEmail = false; + if (form && form.$invalid) { + $scope.error = 'Please enter the required fields'; + return; + } + if (!$scope.hasPin) { + $scope.email = form.email.$modelValue; + $scope.password = form.password.$modelValue; + $scope.setPin = true; + return; + } + $scope.open(form.email.$modelValue, form.password.$modelValue); + }; + + $scope.open = function(email, password) { + $rootScope.starting = true; + identityService.open(email, password, function(err, iden) { + if (err) { $rootScope.starting = false; copay.logger.warn(err); if ((err.toString() || '').match('PNOTFOUND')) { $scope.error = 'Invalid email or password'; + pinService.clear(); } else if ((err.toString() || '').match('Connection')) { $scope.error = 'Could not connect to Insight Server'; } else if ((err.toString() || '').match('Unable')) { diff --git a/js/services/pinService.js b/js/services/pinService.js new file mode 100644 index 000000000..b6d30192c --- /dev/null +++ b/js/services/pinService.js @@ -0,0 +1,38 @@ +'use strict'; + +angular.module('copayApp.services') + .factory('pinService', function($rootScope) { + var root = {}; + var storage = { + pinData: { + pin: 1234, + credentials: { + email: '4@queparece', + password: '1', + } + } + }; + root.check = function() { + return storage.pinData ? true : false; + }; + root.get = function(pin) { + var storedPin = storage.pinData.pin; + if (storedPin !== pin) + return; + + return storage.pinData.credentials; + }; + root.save = function(pin, email, password) { + storage.pinData = { + pin: pin, + credentials: { + email: email, + password: password + } + }; + }; + root.clear = function(){ + delete storage['pinData']; + }; + return root; + }); diff --git a/views/home.html b/views/home.html index 7cae7c3eb..5c7fcff60 100644 --- a/views/home.html +++ b/views/home.html @@ -1,4 +1,4 @@ -
+
@@ -47,9 +47,72 @@ creating your profile
-
+
+

Set your PIN number

+
+
+ + +
+
+ + +
+ +

+ + {{'Pin must match'|translate}} +

+ + +
+ +
+ +
+

Enter your PIN number

+
+

+ + {{error|translate}} +

+
+ + +
+ + +
+ +
+ + +

Sign in to Copay

-
+

@@ -71,6 +134,7 @@ Sign in

+