Wallet/js/controllers/createProfile.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-09-30 21:16:46 -03:00
'use strict';
2014-12-04 20:42:08 -03:00
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService, pinService, isMobile) {
2014-12-03 16:01:22 -03:00
var _credentials, _firstpin;
$scope.init = function() {
identityService.goWalletHome();
2014-12-04 20:42:08 -03:00
$scope.isMobile = isMobile.any();
2014-12-03 16:01:22 -03:00
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;
}
});
};
2014-11-29 18:35:48 -03:00
2014-09-30 21:16:46 -03:00
$scope.createProfile = function(form) {
2014-12-03 16:01:22 -03:00
$rootScope.hideNavigation = false;
2014-10-09 18:53:31 -03:00
if (form && form.$invalid) {
2014-12-03 16:01:22 -03:00
$scope.error = 'Please enter the required fields';
2014-10-09 18:53:31 -03:00
return;
}
2014-12-03 16:01:22 -03:00
$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('');
form.password.$render();
form.repeatpassword.$setViewValue('');
form.repeatpassword.$render();
form.$setPristine();
$scope.error = msg;
},1);
2014-12-03 16:01:22 -03:00
$scope.error = msg;
} else {
$scope.error = null;
2014-12-03 16:01:22 -03:00
// mobile
2014-12-04 20:42:08 -03:00
if ($scope.isMobile) {
2014-12-03 16:01:22 -03:00
_credentials = {
email: form.email.$modelValue,
password: form.password.$modelValue,
};
$scope.askForPin = 1;
$rootScope.hideNavigation = true;
$timeout(function() {
$rootScope.$digest();
}, 1);
return;
} else {
$scope.createDefaultWallet();
}
}
2014-11-30 00:31:17 -03:00
});
2014-09-30 21:16:46 -03:00
}
});