Merge pull request #1930 from cmgustavo/feature/pin-01

PIN for mobile devices
This commit is contained in:
Matias Alejo Garcia 2014-12-03 23:56:10 -03:00
commit 6f1342ea27
21 changed files with 589 additions and 130 deletions

View file

@ -1,23 +1,80 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService) {
identityService.goWalletHome();
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService, pinService) {
var _credentials, _firstpin;
$scope.init = function() {
identityService.goWalletHome();
pinService.makePinInput($scope, 'newpin', function(newValue) {
_firstpin = newValue;
$scope.askForPin = 2;
});
pinService.makePinInput($scope, 'repeatpin', function(newValue) {
if (newValue === _firstpin) {
_firstpin = null;
$scope.createPin(newValue);
} else {
$scope.askForPin = 1;
_firstpin = null;
$scope.setPinForm.newpin.$setViewValue('');
$scope.setPinForm.newpin.$render();
$scope.setPinForm.repeatpin.$setViewValue('');
$scope.setPinForm.repeatpin.$render();
$scope.setPinForm.$setPristine();
$scope.error = 'Entered PINs were not equal. Try again';
}
});
};
$scope.createPin = function(pin) {
preconditions.checkArgument(pin);
preconditions.checkState($rootScope.iden);
preconditions.checkState(_credentials && _credentials.email);
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
$scope.askForPin = 0;
$rootScope.hasPin = true;
$scope.createDefaultWallet();
});
};
$scope.createDefaultWallet = function() {
$rootScope.hideNavigation = false;
identityService.createDefaultWallet(function(err) {
$scope.askForPin =0 ;
$scope.loading = false;
if (err) {
var msg = err.toString();
$scope.error = msg;
}
});
};
$scope.createProfile = function(form) {
$rootScope.hideNavigation = false;
if (form && form.$invalid) {
$scope.error('Error', 'Please enter the required fields');
$scope.error = 'Please enter the required fields';
return;
}
$rootScope.starting = true;
identityService.create(
form.email.$modelValue, form.password.$modelValue, function(err) {
$rootScope.starting = false;
if (err) {
var msg = err.toString();
if (msg.indexOf('EEXIST')>=0 || msg.indexOf('BADC')>=0 ) {
msg = 'This profile already exists'
}
$timeout(function() {
$scope.loading = true;
identityService.create(form.email.$modelValue, form.password.$modelValue, function(err) {
$scope.loading = false;
if (err) {
var msg = err.toString();
if (msg.indexOf('EEXIST') >= 0 || msg.indexOf('BADC') >= 0) {
msg = 'This profile already exists'
}
$timeout(function() {
form.email.$setViewValue('');
form.email.$render();
form.password.$setViewValue('');
@ -27,7 +84,26 @@ angular.module('copayApp.controllers').controller('CreateProfileController', fun
form.$setPristine();
$scope.error = msg;
},1);
}
$scope.error = msg;
} else {
$scope.error = null;
// mobile
if (isMobile.any()) {
_credentials = {
email: form.email.$modelValue,
password: form.password.$modelValue,
};
$scope.askForPin = 1;
$rootScope.hideNavigation = true;
$timeout(function() {
$rootScope.$digest();
}, 1);
return;
} else {
$scope.createDefaultWallet();
}
}
});
}
});

View file

@ -39,9 +39,10 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
window.onbeforeunload = undefined;
});
if ($rootScope.wallet) {
$scope.$on('$idleStart', function() {
});
$scope.init = function() {
if (!$rootScope.wallet) return;
$scope.$on('$idleStart', function() {});
$scope.$on('$idleWarn', function(a, countdown) {
$rootScope.countdown = countdown;
$rootScope.sessionExpired = true;
@ -64,8 +65,5 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
$rootScope.$watch('title', function(newTitle, oldTitle) {
$scope.title = newTitle;
});
$rootScope.$on('signout', function() {
$scope.signout();
});
}
};
});

View file

@ -1,20 +1,57 @@
'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, applicationService, isMobile) {
if ($rootScope.fromEmailConfirmation) {
$scope.confirmedEmail = true;
$rootScope.fromEmailConfirmation = false;
}
var _credentials, _firstpin;
Compatibility.check($scope);
$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.iden) {
identityService.goWalletHome();
}
Compatibility.check($scope);
pinService.check(function(err, value) {
$rootScope.hasPin = value;
});
};
pinService.makePinInput($scope, 'pin', function(newValue) {
$scope.openWithPin(newValue);
});
pinService.makePinInput($scope, 'newpin', function(newValue) {
_firstpin = newValue;
$scope.askForPin = 2;
});
pinService.makePinInput($scope, 'repeatpin', function(newValue) {
if (newValue === _firstpin) {
_firstpin = null;
$scope.createPin(newValue);
} else {
$scope.$$childTail.setPinForm.newpin.$setViewValue('');
$scope.$$childTail.setPinForm.newpin.$render();
$scope.$$childTail.setPinForm.repeatpin.$setViewValue('');
$scope.$$childTail.setPinForm.repeatpin.$render();
_firstpin = null;
$scope.askForPin = 1;
$scope.error = 'Entered PINs were not equal. Try again';
}
});
$scope.done = function() {
$rootScope.starting = false;
@ -22,28 +59,87 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
};
$scope.$on("$destroy", function(){
$scope.$on("$destroy", function() {
var iden = $rootScope.iden;
if (iden) {
iden.removeListener('newWallet', $scope.done );
iden.removeListener('noWallets', $scope.done );
iden.removeListener('newWallet', $scope.done);
iden.removeListener('noWallets', $scope.done);
}
});
$scope.openProfile = function(form) {
$scope.confirmedEmail = false;
$scope.openWithPin = function(pin) {
if (!pin) {
$scope.error = 'Please enter the required fields';
return;
}
var credentials = pinService.get(pin, function(err, credentials) {
if (err || !credentials) {
$scope.error = 'Wrong PIN';
return;
}
$rootScope.starting = true;
$scope.open(credentials.email, credentials.password);
});
};
$scope.openWallets = function() {
preconditions.checkState($rootScope.iden);
var iden = $rootScope.iden;
$rootScope.hideNavigation = false;
$rootScope.starting = true;
iden.on('newWallet', $scope.done);
iden.on('noWallets', $scope.done);
iden.openWallets();
};
$scope.createPin = function(pin) {
preconditions.checkArgument(pin);
preconditions.checkState($rootScope.iden);
preconditions.checkState(_credentials && _credentials.email);
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
$scope.askForPin = 0;
$rootScope.hasPin = true;
$scope.openWallets();
});
};
$scope.openWithCredentials = function(form) {
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
$scope.open(form.email.$modelValue, form.password.$modelValue);
};
$scope.pinLogout = function() {
pinService.clear(function() {
copay.logger.debug('PIN erased');
delete $rootScope['hasPin'];
applicationService.reload();
});
};
$scope.open = function(email, password) {
$rootScope.starting = true;
identityService.open(form.email.$modelValue, form.password.$modelValue, function(err, iden) {
if (err) {
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(function() {
copay.logger.debug('PIN erased');
});
} else if ((err.toString() || '').match('Connection')) {
$scope.error = 'Could not connect to Insight Server';
} else if ((err.toString() || '').match('Unable')) {
@ -51,13 +147,33 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
} else {
$scope.error = 'Unknown error';
}
return $scope.done();
$rootScope.starting = false;
$rootScope.$digest();
return;
}
// Open successfully?
if (iden) {
iden.on('newWallet', $scope.done);
iden.on('noWallets', $scope.done);
iden.openWallets();
$scope.error = null;
$scope.confirmedEmail = false;
// mobile
if (isMobile.any() && !$rootScope.hasPin) {
$scope.done();
_credentials = {
email: email,
password: password,
};
$scope.askForPin = 1;
$rootScope.starting = false;
$rootScope.hideNavigation = true;
$rootScope.$digest();
return;
}
// no mobile
else {
$scope.openWallets();
}
}
});
}

View file

@ -24,8 +24,13 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
'link': 'more'
}];
$scope.go = function (path) {
$location.path(path);
};
$scope.signout = function() {
$scope.$emit('signout');
$rootScope.signingOut = true;
identityService.signout();
};
$scope.isActive = function(item) {
@ -47,7 +52,7 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
$scope.init = function() {
// This should be called only once.
// focused wallet change
if ($rootScope.wallet) {
$rootScope.$watch('wallet', function() {
@ -72,16 +77,14 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
if (newWid && $rootScope.iden.getWalletById(newWid)) {
identityService.setFocusedWallet(newWid);
} else {
copay.logger.debug('No wallets');
copay.logger.debug('No wallets');
identityService.noFocusedWallet(newWid);
}
}
$scope.walletSelection = false;
$scope.setWallets();
});
}
};
$scope.setWallets = function() {