Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong locale message picked up after successfull 2fa authentication. #227

Open
artur79 opened this issue Oct 4, 2022 · 3 comments
Open

Comments

@artur79
Copy link

artur79 commented Oct 4, 2022

After successful 2fa authentication, devise basically for some reason device grabs warning from locale path devise.failure.already_authenticated instead notice from devise.sessions.signed_in

Here's my controller concern:

module AuthenticateWithOtpTwoFactor
  extend ActiveSupport::Concern

  def authenticate_with_otp_two_factor
    user = self.resource = find_user

    if user_params[:otp_attempt].present? && session[:otp_user_id]
      authenticate_user_with_otp_two_factor(user)
    elsif user&.valid_password?(user_params[:password])
      prompt_for_otp_two_factor(user)
    end
  end

  private

  def valid_otp_attempt?(user)
    user.validate_and_consume_otp!(user_params[:otp_attempt]) # || user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  rescue StandardError => e
    Rails.logger.error e
    false
  end

  def prompt_for_otp_two_factor(user)
    @user = user

    session[:otp_user_id] = user.id
    render 'devise/sessions/two_factor'
  end

  def authenticate_user_with_otp_two_factor(user)
    if valid_otp_attempt?(user)
      # Remove any lingering user data from login
      session.delete(:otp_user_id)

      # remember_me(user) if user_params[:remember_me] == '1' we dont use remember me atm
      user.save!
      sign_in(user, event: :authentication)
    else
      flash.now[:alert] = 'Invalid code.'
      prompt_for_otp_two_factor(user)
    end
  end

  def user_params
    params.require(:user).permit(:login, :email, :password, :remember_me, :otp_attempt)
  end

  def find_user
    if session[:otp_user_id]
      User.unscoped.find(session[:otp_user_id])
    elsif user_params[:login]
      User.unscoped.where(email: user_params[:login])&.first
    end
  end

  def otp_two_factor_enabled?
    Tenant.current.has_2fa? && find_user&.otp_required_for_login
  end
end
@nonuabi
Copy link

nonuabi commented Dec 29, 2022

Hi @artur79 , I'm having the same issue. Have you found any alternate solutions for it yet?

@artur79
Copy link
Author

artur79 commented Dec 29, 2022

nope

@siobhan559
Copy link

Hi, has anyone found a solution for this? Having the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants