From a9e9093de8e1c0564d4bb1ed97ca693274b51409 Mon Sep 17 00:00:00 2001 From: Kevin Robatel Date: Thu, 8 Feb 2018 11:52:13 +0100 Subject: [PATCH] Return JSON with 'redirect_to' when handle_failed_second_factor --- lib/two_factor_authentication/controllers/helpers.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/two_factor_authentication/controllers/helpers.rb b/lib/two_factor_authentication/controllers/helpers.rb index f8a084d..c3b01f6 100644 --- a/lib/two_factor_authentication/controllers/helpers.rb +++ b/lib/two_factor_authentication/controllers/helpers.rb @@ -20,9 +20,14 @@ module TwoFactorAuthentication end def handle_failed_second_factor(scope) - if request.format.present? and request.format.html? - session["#{scope}_return_to"] = request.original_fullpath if request.get? - redirect_to two_factor_authentication_path_for(scope) + if request.format.present? + if request.format.html? + session["#{scope}_return_to"] = request.original_fullpath if request.get? + redirect_to two_factor_authentication_path_for(scope) + elsif request.format.json? + session["#{scope}_return_to"] = root_path(format: :html) + render json: { redirect_to: two_factor_authentication_path_for(scope) }, status: :unauthorized + end else render nothing: true, status: :unauthorized end