copay/js/services/identityService.js

84 lines
2.5 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.services')
2014-10-27 07:10:32 -07:00
.factory('identityService', function($rootScope, $location, pluginManager, controllerUtils) {
var root = {};
2014-10-30 14:08:50 -07:00
root.check = function(scope) {
copay.Identity.checkIfExistsAny({
pluginManager: pluginManager,
}, function(anyProfile) {
copay.Wallet.checkIfExistsAny({
pluginManager: pluginManager,
}, function(anyWallet) {
scope.retreiving = false;
scope.anyProfile = anyProfile ? true : false;
scope.anyWallet = anyWallet ? true : false;
if (!scope.anyProfile) {
$location.path('/createProfile');
}
});
});
};
2014-10-27 07:10:32 -07:00
root.create = function(scope, form) {
2014-10-30 14:08:50 -07:00
copay.Identity.create({
2014-10-27 07:10:32 -07:00
email: form.email.$modelValue,
password: form.password.$modelValue,
pluginManager: pluginManager,
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
2014-10-30 14:08:50 -07:00
failIfExists: true,
}, function(err, iden) {
if (err || !iden) {
controllerUtils.onErrorDigest(scope, 'User already exists!');
return;
}
2014-10-30 14:08:50 -07:00
var walletOptions = {
nickname: iden.fullName,
networkName: config.networkName,
requiredCopayers: 1,
totalCopayers: 1,
password: iden.password,
name: 'My wallet',
};
iden.createWallet(walletOptions, function(err, wallet) {
if (err || !wallet) {
controllerUtils.onErrorDigest(scope, 'Could not create default wallet');
return;
}
scope.loading = false;
controllerUtils.bindProfile(scope, iden, wallet.id);
});
2014-10-27 07:10:32 -07:00
});
2014-10-30 14:08:50 -07:00
2014-10-27 07:10:32 -07:00
};
2014-10-24 07:58:22 -07:00
2014-10-27 07:10:32 -07:00
root.open = function(scope, form) {
copay.Identity.open({
email: form.email.$modelValue,
password: form.password.$modelValue,
pluginManager: pluginManager,
network: config.network,
networkName: config.networkName,
walletDefaults: config.wallet,
passphraseConfig: config.passphraseConfig,
}, function(err, iden) {
if (err && !iden) {
controllerUtils.onErrorDigest(
scope, (err.toString() || '').match('PNOTFOUND') ? 'Invalid email or password' : 'Unknown error');
2014-10-27 07:10:32 -07:00
} else {
var firstWallet = iden.getLastFocusedWallet();
controllerUtils.bindProfile(scope, iden, firstWallet);
}
scope.loading = false;
});
2014-10-30 14:08:50 -07:00
};
2014-10-27 07:10:32 -07:00
return root;
});