copay/js/services/identityService.js

94 lines
2.8 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) {
2014-10-31 07:24:16 -07:00
scope.loading = false;
2014-10-30 14:08:50 -07:00
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) {
scope.loading = false;
2014-10-30 14:08:50 -07:00
if (err || !iden) {
copay.logger.debug(err);
if (err && ( err.match('EEXISTS') || err.match('BADCREDENTIALS'))) {
scope.error = 'User already exists!';
} else {
scope.error = 'Unknown error when connecting Insight Server';
}
2014-10-30 14:08:50 -07:00
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) {
copay.logger.debug(err);
scope.error = 'Could not create default wallet';
2014-10-30 14:08:50 -07:00
return;
}
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) {
if ((err.toString() || '').match('PNOTFOUND')) {
scope.error = 'Invalid email or password';
}
else {
scope.error = '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;
});