attempts limit - remaining time

This commit is contained in:
JDonadio 2017-03-30 11:31:23 -03:00
commit c29af5f658
4 changed files with 101 additions and 12 deletions

View file

@ -1,22 +1,93 @@
'use strict';
angular.module('copayApp.controllers').controller('pinController', function($state, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
var PIN = 'pin';
var ATTEPMPTS_LIMIT = 3;
$scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
$scope.match = false;
$scope.error = false;
$scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0;
$scope.appName = appConfigService.name;
});
$scope.$on("$ionicView.enter", function(event) {
configService.whenAvailable(function(config) {
$scope.bannedUntil = config.lock.bannedUntil || null;
if ($scope.bannedUntil) {
var now = Math.floor(Date.now() / 1000);
if (now < $scope.bannedUntil) {
$scope.error = $scope.disableButtons = true;
lockTimeControl($scope.bannedUntil);
}
}
});
});
function checkAttempts() {
$scope.currentAttempts += 1;
$log.debug('Attempts to unlock:', $scope.currentAttempts);
if ($scope.currentAttempts === 3) {
$scope.currentAttempts = 0;
var limitTime = Math.floor(Date.now() / 1000) + 5 * 60;
var config = configService.getSync();
var opts = {
lock: {
method: PIN,
value: config.lock.value,
bannedUntil: limitTime,
attempts: config.lock.attempts + 1,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
}
};
function lockTimeControl(limitTime) {
$scope.limitTimeExpired = false;
setExpirationTime();
var countDown = $interval(function() {
setExpirationTime();
}, 1000);
function setExpirationTime() {
var now = Math.floor(Date.now() / 1000);
if (now > limitTime) {
$scope.limitTimeExpired = true;
if (countDown) reset();
} else {
$scope.disableButtons = true;
var totalSecs = limitTime - now;
var m = Math.floor(totalSecs / 60);
var s = totalSecs % 60;
$scope.expires = ('0' + m).slice(-2) + ":" + ('0' + s).slice(-2);
}
};
function reset() {
$scope.expires = $scope.error = $scope.disableButtons = null;
$scope.currentPin = $scope.confirmPin = '';
$interval.cancel(countDown);
$timeout(function() {
$scope.$apply();
});
return;
};
};
$scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null;
};
$scope.delete = function() {
if ($scope.disableButtons) return;
if ($scope.currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1);
$scope.error = false;
@ -30,6 +101,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
};
$scope.updatePin = function(value) {
if ($scope.disableButtons) return;
$scope.error = false;
if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value;
@ -46,11 +118,17 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
$scope.match = config.lock && config.lock.method == PIN && config.lock.value == $scope.currentPin ? true : false;
if (!$scope.locking) {
if ($scope.match) {
$scope.fromSettings ? saveSettings() : $scope.close(150);
$scope.error = false;
if ($scope.fromSettings) saveSettings();
else {
saveSettings(PIN, $scope.currentPin);
$scope.error = false;
}
} else {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
$timeout(function() {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}, 200);
checkAttempts();
}
} else {
processCodes();
@ -59,8 +137,8 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
function processCodes() {
if (!$scope.confirmPin) {
$scope.confirmPin = $scope.currentPin;
$timeout(function() {
$scope.confirmPin = $scope.currentPin;
$scope.currentPin = '';
}, 200);
} else {
@ -77,10 +155,13 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
};
function saveSettings(method, value) {
var config = configService.getSync();
var opts = {
lock: {
method: method || '',
value: value || '',
bannedUntil: null,
attempts: config.lock.attempts + 1,
}
};

View file

@ -56,6 +56,8 @@ angular.module('copayApp.services').factory('configService', function(storageSer
lock: {
method: '',
value: '',
bannedUntil: null,
attempts: null,
},
// External services

View file

@ -25,9 +25,11 @@
.block-text {
align-items: center;
background-color: #F1F1F1;
text-align: center;
height: 30%;
border-bottom: 1px solid #c5c5c5;
.message {
margin: auto;
}
span {
width: 60%;
margin: 10% auto;
@ -88,5 +90,6 @@
}
.error {
color: #f13333;
max-width: 70%;
}
}

View file

@ -5,9 +5,12 @@
</ion-nav-bar>
<div class="content">
<div class="block-text row">
<span translate ng-if="!confirmPin && !error">Please enter your mobile unlock code</span>
<span translate ng-if="confirmPin && !error">Confirm your mobile unlock code</span>
<span translate ng-if="error" class="error">Incorrect code, try again.</span>
<div class="message" ng-if="!confirmPin && !error" translate>Please enter your unlock PIN</div>
<div class="message" ng-if="confirmPin && !error" translate>Confirm your unlock PIN</div>
<div class="message error" ng-if="error">
<div ng-if="!expires" translate>Incorrect PIN, try again.</div>
<time ng-if="expires" translate>You have incorrectly typed your PIN. Try again in {{expires}}</time>
</div>
</div>
<div class="app-icon">
<i class="icon big-icon-svg">