refactor iden #create

This commit is contained in:
Matias Alejo Garcia 2014-09-30 15:28:02 -03:00
commit 606ea0668c
10 changed files with 72 additions and 54 deletions

View file

@ -1,12 +1,12 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, identity, notification, controllerUtils) {
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils) {
controllerUtils.redirIfLogged();
$scope.retreiving = true;
identity.getWallets(function(err,ret) {
$scope.retreiving = false;
$scope.hasWallets = (ret && ret.length > 0) ? true : false;
});
//$scope.retreiving = true;
// identity.getWallets(function(err,ret) {
// $scope.retreiving = false;
// $scope.hasWallets = (ret && ret.length > 0) ? true : false;
// });
});

View file

@ -57,15 +57,12 @@ function Identity(email, password, opts) {
// open wallets
this.wallets = [];
this.profile = Identity._newProfile({
email: email,
}, password, this.storage);
};
/* for stubbing */
Identity._newProfile = function(info, password, storage) {
return new Profile(info, password, storage);
Identity._createProfile = function(email, password, storage, cb) {
Profile.create(email, password, storage, cb);
};
Identity._newInsight = function(opts) {
@ -93,7 +90,7 @@ Identity._walletDelete = function(id, cb) {
};
Identity._profileOpen = function(e, p, s, cb) {
return Profile.open(e, p, s, cb);
Profile.create(e, p, s, cb);
};
@ -109,11 +106,13 @@ Identity._profileOpen = function(e, p, s, cb) {
* @return {undefined}
*/
Identity.create = function(email, password, opts, cb) {
var iden = new Identity(email, password, opts);
iden.store({
overwrite: false,
}, function(err) {
return cb(err, iden);
Identity._createProfile(email, password, iden.storage, function(err, profile) {
if (err) return cb(err);
iden.profile = profile;
return cb(null, iden);
});
};
@ -144,7 +143,7 @@ Identity.prototype.validate = function(authcode, cb) {
Identity.open = function(email, password, opts, cb) {
var iden = new Identity(email, password, opts);
Identity._profileOpen(email, password, iden.storage, function(err, profile){
Identity._profileOpen(email, password, iden.storage, function(err, profile) {
if (err) return cb(err);
iden.profile = profile;
@ -174,6 +173,8 @@ Identity.isAvailable = function(email, opts, cb) {
* @return {undefined}
*/
Identity.prototype.store = function(opts, cb) {
preconditions.checkState(this.profile);
var self = this;
self.profile.store(opts, function(err) {
if (err) return cb(err);

View file

@ -31,8 +31,8 @@ PluginManager.prototype._register = function(obj, name) {
var type = obj.type;
var kind = PluginManager.TYPE[type];
preconditions.checkArgument(kind, 'Plugin has unknown type' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name);
preconditions.checkArgument(kind, 'Unknown plugin type:' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered:' + name);
if (kind === PluginManager.KIND_UNIQUE) {
this.registered[type] = obj;

View file

@ -24,7 +24,18 @@ Profile.hash = function(email, password) {
};
Profile.key = function(hash) {
return 'identity::' + hash;
return 'profile::' + hash;
};
Profile.create = function(email, password, storage, cb) {
preconditions.checkArgument(cb);
var p = new Profile({
email: email,
hash: Profile.hash(email,password),
}, storage);
p.store(cb);
};
Profile.open = function(email, password, storage, cb) {

View file

@ -10,6 +10,10 @@ angular
templateUrl: 'views/home.html',
validate: false
})
.when('/createProfile', {
templateUrl: 'views/createProfile.html',
validate: false
})
.when('/open', {
templateUrl: 'views/open.html',
validate: false

View file

@ -1,5 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('identity', function(pluginManager){
return new copay.Identity(config, copay.version, pluginManager);
});