copay/js/controllers/createProfile.js

184 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-09-30 17:16:46 -07:00
'use strict';
2014-12-09 11:20:19 -08:00
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, $timeout, notification, pluginManager, identityService, pinService, isMobile, configService, go) {
2014-12-03 11:01:22 -08:00
var _credentials, _firstpin;
$scope.init = function() {
if ($rootScope.wallet)
go.walletHome();
2014-12-06 14:56:51 -08:00
$scope.isMobile = isMobile.any();
2014-12-03 11:01:22 -08:00
2014-12-04 18:32:02 -08:00
$scope.createStep = 'storage';
$scope.useLocalstorage = false;
2014-12-03 11:01:22 -08: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);
2014-12-09 13:49:37 -08:00
$scope.loading = true;
2014-12-03 11:01:22 -08:00
2014-12-10 22:41:28 -08:00
$timeout(function() {
$rootScope.$digest();
pinService.save(pin, _credentials.email, _credentials.password, function(err) {
_credentials.password = '';
_credentials = null;
$scope.askForPin = 0;
$rootScope.hasPin = true;
$scope.loading = null;
$scope.createDefaultWallet();
});
}, 1);
2014-12-03 11:01:22 -08:00
};
2014-12-05 09:53:54 -08:00
$scope.setStep = function(step) {
$scope.error = null;
$scope.createStep = step;
$timeout(function() {
$scope.$digest();
}, 1);
};
$scope.selectStorage = function(storage) {
2014-12-05 07:20:43 -08:00
$scope.useLocalstorage = storage == 'local';
$timeout(function() {
$scope.$digest();
}, 1);
};
2014-12-05 09:23:33 -08:00
$scope.goToEmail = function() {
2014-12-04 18:32:02 -08:00
$scope.createStep = 'email';
2014-12-05 09:23:33 -08:00
$scope.useEmail = !$scope.useLocalstorage;
2014-12-04 18:32:02 -08:00
};
$scope.setEmailOrUsername = function(form) {
$scope.userOrEmail = $scope.useLocalstorage ? form.username.$modelValue : form.email.$modelValue;
preconditions.checkState($scope.userOrEmail);
2014-12-06 14:56:51 -08:00
$scope.error = null;
2014-12-04 18:32:02 -08:00
$scope.createStep = 'pass';
$timeout(function() {
$scope.$digest();
}, 1);
};
2014-12-03 11:01:22 -08:00
$scope.createDefaultWallet = function() {
$rootScope.hideNavigation = false;
identityService.createDefaultWallet(function(err) {
2014-12-04 18:32:02 -08:00
$scope.askForPin = 0;
2014-12-03 11:01:22 -08:00
$scope.loading = false;
if (err) {
var msg = err.toString();
$scope.error = msg;
}
});
};
2014-11-29 13:35:48 -08:00
2014-12-05 09:23:33 -08:00
2014-12-05 09:53:54 -08:00
$scope._doCreateProfile = function(emailOrUsername, password, cb) {
2014-12-05 09:23:33 -08:00
preconditions.checkArgument(_.isString(emailOrUsername));
preconditions.checkArgument(_.isString(password));
2014-12-05 09:53:54 -08:00
$rootScope.hideNavigation = false;
$scope.loading = true;
2014-12-05 09:23:33 -08:00
identityService.create(emailOrUsername, password, function(err) {
2014-12-09 13:49:37 -08:00
$scope.loading = null;
2014-12-05 09:53:54 -08:00
$scope.error = null;
2014-12-03 11:01:22 -08:00
if (err) {
var msg = err.toString();
if (msg.indexOf('EEXIST') >= 0 || msg.indexOf('BADC') >= 0) {
msg = 'This profile already exists'
$scope.createStep = 'email';
2014-12-03 11:01:22 -08:00
}
$scope.error = msg;
} else {
// mobile
2014-12-04 15:42:08 -08:00
if ($scope.isMobile) {
2014-12-03 11:01:22 -08:00
_credentials = {
email: emailOrUsername,
password: password,
2014-12-03 11:01:22 -08:00
};
$scope.askForPin = 1;
$rootScope.hideNavigation = true;
$timeout(function() {
$rootScope.$digest();
}, 1);
return;
} else {
$scope.createDefaultWallet();
}
}
2014-12-05 09:53:54 -08:00
return cb();
2014-11-29 19:31:17 -08:00
});
2014-12-05 09:23:33 -08:00
};
2014-12-05 09:53:54 -08:00
$scope.saveSettings = function(cb) {
2014-12-05 09:23:33 -08:00
var plugins = config.plugins;
plugins.EncryptedLocalStorage = false;
plugins.EncryptedInsightStorage = false;
var pluginName = $scope.useLocalstorage ? 'EncryptedLocalStorage' : 'EncryptedInsightStorage';
plugins[pluginName] = true;
configService.set({
plugins: plugins
2014-12-05 09:53:54 -08:00
}, cb);
};
$scope.createProfile = function(form) {
if (form && form.$invalid) {
$scope.error = 'Please enter the required fields';
return;
}
$scope.saveSettings(function(err) {
2014-12-10 22:41:28 -08:00
preconditions.checkState(!err, err);
2014-12-06 16:07:26 -08:00
2014-12-05 09:53:54 -08:00
$scope._doCreateProfile($scope.userOrEmail, form.password.$modelValue, function(err) {
$timeout(function() {
form.password.$setViewValue('');
form.password.$render();
form.repeatpassword.$setViewValue('');
form.repeatpassword.$render();
form.$setPristine();
}, 1);
});
2014-12-05 09:23:33 -08:00
});
};
2014-09-30 17:16:46 -07:00
});