Commit aa4f0851 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'rs-refactor-2fa' into 'master'

Refactor SessionsController to use a controller concern

See merge request !659
parents 37bc4bb1 c802d8ee
No related merge requests found
# == AuthenticatesWithTwoFactor
#
# Controller concern to handle two-factor authentication
#
# Upon inclusion, skips `require_no_authentication` on `:create`.
module AuthenticatesWithTwoFactor
extend ActiveSupport::Concern
included do
# This action comes from DeviseController, but because we call `sign_in`
# manually, not skipping this action would cause a "You are already signed
# in." error message to be shown upon successful login.
skip_before_action :require_no_authentication, only: [:create]
end
# Store the user's ID in the session for later retrieval and render the
# two factor code prompt
#
# The user must have been authenticated with a valid login and password
# before calling this method!
#
# user - User record
#
# Returns nil
def prompt_for_two_factor(user)
session[:otp_user_id] = user.id
render 'devise/sessions/two_factor' and return
end
end
class SessionsController < Devise::SessionsController class SessionsController < Devise::SessionsController
prepend_before_action :authenticate_with_two_factor, only: [:create] include AuthenticatesWithTwoFactor
# This action comes from DeviseController, but because we call `sign_in` prepend_before_action :authenticate_with_two_factor, only: [:create]
# manually inside `authenticate_with_two_factor`, not skipping this action
# would cause a "You are already signed in." error message to be shown upon
# successful login.
skip_before_action :require_no_authentication, only: [:create]
def new def new
redirect_path = redirect_path =
...@@ -74,9 +70,7 @@ class SessionsController < Devise::SessionsController ...@@ -74,9 +70,7 @@ class SessionsController < Devise::SessionsController
end end
else else
if user && user.valid_password?(user_params[:password]) if user && user.valid_password?(user_params[:password])
# Save the user's ID to session so we can ask for a one-time password prompt_for_two_factor(user)
session[:otp_user_id] = user.id
render :two_factor and return
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment