From 43d40b3ab8fbdd37216729a3c9356d284154c806 Mon Sep 17 00:00:00 2001 From: Evan Sharp Date: Fri, 5 Feb 2016 21:25:10 -0500 Subject: [PATCH] Use recaptcha expired called Resolves #57 by using recaptcha's expired-callback instead of a hard-coded 2 min timeout --- src/directive.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/directive.js b/src/directive.js index 0237f4a..38b281e 100644 --- a/src/directive.js +++ b/src/directive.js @@ -31,7 +31,6 @@ scope.widgetId = null; - var sessionTimeout; var removeCreationListener = scope.$watch('key', function (key) { if (!key) { return; @@ -51,16 +50,6 @@ // Notify about the response availability scope.onSuccess({response: gRecaptchaResponse, widgetId: scope.widgetId}); }); - - // captcha session lasts 2 mins after set. - sessionTimeout = $timeout(function (){ - if(ctrl){ - ctrl.$setValidity('recaptcha',false); - } - scope.response = ""; - // Notify about the response availability - scope.onExpire({widgetId: scope.widgetId}); - }, 2 * 60 * 1000); }; vcRecaptcha.create(elm[0], key, callback, { @@ -68,7 +57,8 @@ stoken: scope.stoken || attrs.stoken || null, theme: scope.theme || attrs.theme || null, tabindex: scope.tabindex || attrs.tabindex || null, - size: scope.size || attrs.size || null + size: scope.size || attrs.size || null, + 'expired-callback': expired }).then(function (widgetId) { // The widget has been created @@ -91,14 +81,19 @@ // reset the validity of the form if we were removed ctrl.$setValidity('recaptcha', null); } - if (sessionTimeout) { - // don't trigger the session timeout if we are no longer active - $timeout.cancel(sessionTimeout); - sessionTimeout = null; - } + cleanup(); } + function expired(){ + if(ctrl){ + ctrl.$setValidity('recaptcha',false); + } + scope.response = ""; + // Notify about the response availability + scope.onExpire({widgetId: scope.widgetId}); + } + function cleanup(){ // removes elements reCaptcha added. angular.element($document[0].querySelectorAll('.pls-container')).parent().remove();