2014-07-23 17:10:02 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, $timeout, notification, identityService, Compatibility, pinService) {
|
2014-11-01 21:34:03 -03:00
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
$scope.init = function() {
|
|
|
|
|
// This is only for backwards compat, insight api should link to #!/confirmed directly
|
|
|
|
|
if (getParam('confirmed')) {
|
|
|
|
|
var hashIndex = window.location.href.indexOf('/?');
|
|
|
|
|
window.location = window.location.href.substr(0, hashIndex) + '#!/confirmed';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-11-01 21:34:03 -03:00
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
if ($rootScope.fromEmailConfirmation) {
|
|
|
|
|
$scope.confirmedEmail = true;
|
|
|
|
|
$rootScope.fromEmailConfirmation = false;
|
|
|
|
|
}
|
|
|
|
|
Compatibility.check($scope);
|
2014-12-02 22:38:33 -03:00
|
|
|
pinService.check(function(err, value) {
|
|
|
|
|
$scope.hasPin = value;
|
|
|
|
|
});
|
2014-12-02 16:16:31 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.defineProperty($scope,
|
|
|
|
|
"pin", {
|
|
|
|
|
get: function() {
|
|
|
|
|
return this._pin;
|
|
|
|
|
},
|
2014-12-02 22:38:33 -03:00
|
|
|
set: function(newValue) {
|
|
|
|
|
this._pin = newValue;
|
|
|
|
|
if (newValue && newValue.length == 4) {
|
|
|
|
|
$scope.openPin(newValue);
|
|
|
|
|
}
|
|
|
|
|
if (!newValue) {
|
|
|
|
|
$scope.error = null;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
enumerable: true,
|
|
|
|
|
configurable: true
|
2014-12-02 16:16:31 -03:00
|
|
|
});
|
2014-11-01 21:34:03 -03:00
|
|
|
|
2014-10-30 16:13:56 -03:00
|
|
|
|
2014-11-30 23:14:46 -03:00
|
|
|
$scope.done = function() {
|
|
|
|
|
$rootScope.starting = false;
|
2014-12-01 16:19:34 -03:00
|
|
|
$rootScope.$digest();
|
2014-11-30 23:14:46 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
$scope.$on("$destroy", function() {
|
2014-11-30 23:14:46 -03:00
|
|
|
var iden = $rootScope.iden;
|
|
|
|
|
if (iden) {
|
2014-12-02 22:38:33 -03:00
|
|
|
iden.removeListener('newWallet', $scope.done);
|
|
|
|
|
iden.removeListener('noWallets', $scope.done);
|
2014-11-30 23:14:46 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
$scope.openWithPin = function(form) {
|
|
|
|
|
$scope.confirmedEmail = false;
|
|
|
|
|
if (form && form.$invalid) {
|
|
|
|
|
$scope.error = 'Please enter the required fields';
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-12-02 22:38:33 -03:00
|
|
|
$scope.openPin(pin);
|
2014-12-02 16:16:31 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.openPin = function(pin) {
|
2014-12-02 22:38:33 -03:00
|
|
|
var credentials = pinService.get(pin, function(err, credentials) {
|
|
|
|
|
if (err || !credentials) {
|
|
|
|
|
$scope.error = 'Wrong PIN';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$rootScope.starting = true;
|
|
|
|
|
$scope.open(credentials.email, credentials.password);
|
|
|
|
|
});
|
2014-12-02 16:16:31 -03:00
|
|
|
};
|
2014-11-30 23:14:46 -03:00
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
$scope.createPin = function(form) {
|
2014-12-02 22:38:33 -03:00
|
|
|
if (!form) return;
|
|
|
|
|
|
|
|
|
|
pinService.save(form.repeatpin.$modelValue, $scope.email, $scope.password, function(err) {
|
|
|
|
|
$scope.open($scope.email, $scope.password);
|
|
|
|
|
});
|
2014-12-02 16:16:31 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.openWithCredentials = function(form) {
|
2014-11-01 21:34:03 -03:00
|
|
|
$scope.confirmedEmail = false;
|
2014-10-09 18:53:31 -03:00
|
|
|
if (form && form.$invalid) {
|
2014-11-01 21:34:03 -03:00
|
|
|
$scope.error = 'Please enter the required fields';
|
2014-10-09 18:53:31 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2014-12-02 16:16:31 -03:00
|
|
|
if (!$scope.hasPin) {
|
|
|
|
|
$scope.email = form.email.$modelValue;
|
|
|
|
|
$scope.password = form.password.$modelValue;
|
|
|
|
|
$scope.setPin = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.open(form.email.$modelValue, form.password.$modelValue);
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-02 22:38:33 -03:00
|
|
|
|
|
|
|
|
$scope.pinLogout = function() {
|
|
|
|
|
pinService.clear(function() {
|
|
|
|
|
copay.logger.debug('PIN erased');
|
|
|
|
|
$scope.hasPin = null;
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-02 16:16:31 -03:00
|
|
|
$scope.open = function(email, password) {
|
2014-11-30 00:31:17 -03:00
|
|
|
$rootScope.starting = true;
|
2014-12-02 16:16:31 -03:00
|
|
|
identityService.open(email, password, function(err, iden) {
|
|
|
|
|
if (err) {
|
2014-11-30 23:14:46 -03:00
|
|
|
$rootScope.starting = false;
|
2014-11-30 00:31:17 -03:00
|
|
|
copay.logger.warn(err);
|
|
|
|
|
if ((err.toString() || '').match('PNOTFOUND')) {
|
|
|
|
|
$scope.error = 'Invalid email or password';
|
2014-12-02 22:38:33 -03:00
|
|
|
pinService.clear(function() {
|
|
|
|
|
copay.logger.debug('PIN erased');
|
|
|
|
|
});
|
2014-12-02 13:52:17 -03:00
|
|
|
} else if ((err.toString() || '').match('Connection')) {
|
|
|
|
|
$scope.error = 'Could not connect to Insight Server';
|
2014-12-03 08:23:53 -03:00
|
|
|
} else if ((err.toString() || '').match('Unable')) {
|
|
|
|
|
$scope.error = 'Unable to read data from the Insight Server';
|
2014-11-30 00:31:17 -03:00
|
|
|
} else {
|
|
|
|
|
$scope.error = 'Unknown error';
|
|
|
|
|
}
|
2014-12-01 16:19:34 -03:00
|
|
|
return $scope.done();
|
2014-11-30 00:31:17 -03:00
|
|
|
}
|
2014-11-30 23:14:46 -03:00
|
|
|
|
|
|
|
|
if (iden) {
|
|
|
|
|
iden.on('newWallet', $scope.done);
|
|
|
|
|
iden.on('noWallets', $scope.done);
|
2014-12-01 15:33:16 -03:00
|
|
|
iden.openWallets();
|
2014-11-30 23:14:46 -03:00
|
|
|
}
|
2014-11-30 00:31:17 -03:00
|
|
|
});
|
2014-10-01 08:35:17 -03:00
|
|
|
}
|
2014-10-30 12:06:48 -03:00
|
|
|
|
|
|
|
|
function getParam(sname) {
|
|
|
|
|
var params = location.search.substr(location.search.indexOf("?") + 1);
|
|
|
|
|
var sval = "";
|
|
|
|
|
params = params.split("&");
|
|
|
|
|
// split param and value into individual pieces
|
|
|
|
|
for (var i = 0; i < params.length; i++) {
|
|
|
|
|
var temp = params[i].split("=");
|
|
|
|
|
if ([temp[0]] == sname) {
|
|
|
|
|
sval = temp[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sval;
|
|
|
|
|
}
|
2014-09-03 01:25:08 -03:00
|
|
|
});
|