Skip to content

Commit

Permalink
Resend code in authorization wqhen the code is not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
sinaeftekhar committed Sep 22, 2023
1 parent 5cee554 commit 2cdbd0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,40 @@ module Decidim
module HelsinkiSmsauth
module Verifications
class ConfirmUserPhoneAuthorization < ::Decidim::Verifications::ConfirmUserAuthorization
def call
return already_confirmed! if authorization.granted?

return invalid! unless form.valid?

throttle! if too_many_failed_attempts?
if confirmation_successful?
if code_still_valid?
valid!
else
expire!
end
else
invalid!
end
rescue StandardError => e
puts e
invalid!(e.message)
end

private

def valid!
reset_failed_attempts!
broadcast(:ok)
end

def code_still_valid?
authorization.verification_metadata["code_sent_at"].in_time_zone >= 10.minutes.ago
end

def expire!
broadcast(:expired)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def authenticate_user
end

on(:expired) do
flash[:alert] = I18n.t(".expired", scope: "decidim.helsinki_smsauth.omniauth.authenticate_user")
redirect_to action: "resend_code"
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def create
@form = AuthorizationForm.from_params(params.merge(user: current_user, school: nil, grade: nil, organization: current_organization))
Decidim::Verifications::PerformAuthorizationStep.call(authorization, @form) do
on(:ok) do
update_attempt_session
flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.sms")
redirect_to redirect_smsauth
end
Expand All @@ -50,11 +49,10 @@ def resend_code
return unless eligible_to?

@form = AuthorizationForm.from_params(params.merge(user: current_user, organization: current_organization).merge(authorization_params))

last_request_time = last_request
Decidim::Verifications::PerformAuthorizationStep.call(authorization, @form) do
on(:ok) do
update_attempt_session
flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.sms")
flash_message_for_resend(last_request_time)
authorization_method = Decidim::Verifications::Adapter.from_element(authorization.name)
redirect_to authorization_method.resume_authorization_path(redirect_url: redirect_url)
end
Expand Down Expand Up @@ -95,6 +93,9 @@ def update
flash[:error] = t("update.incorrect", scope: "decidim.helsinki_smsauth.verification.authorizations")
redirect_to action: :edit
end
on(:expired) do
redirect_to action: "resend_code", expired: true
end
end
end

Expand Down Expand Up @@ -138,18 +139,6 @@ def authorization
)
end

def attempt_session
session[:last_attempt]
end

def update_attempt_session
session[:last_attempt] = Time.current
end

def expired?
attempt_session <= 1.minute.ago
end

def update_current_user!
current_user.update(authorization_params)
end
Expand All @@ -165,13 +154,17 @@ def verification_code
end

def eligible_to?
return true if expired?
return true if ensure_sending_limit

flash[:error] = I18n.t(".not_allowed", scope: "decidim.helsinki_smsauth.omniauth.send_message")
redirect_to redirect_smsauth
false
end

def ensure_sending_limit
authorization.verification_metadata["code_sent_at"] < 1.minute.ago
end

def redirect_smsauth
authorization_method = Decidim::Verifications::Adapter.from_element(authorization.name)
authorization_method.resume_authorization_path(redirect_url: redirect_url)
Expand All @@ -190,6 +183,22 @@ def handle_redirect
end
end
end

def flash_message_for_resend(last_request)
if long_ago?(last_request)
flash[:alert] = I18n.t(".expired", scope: "decidim.helsinki_smsauth.omniauth.send_message")
else
flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.sms")
end
end

def long_ago?(last_attempt)
last_attempt <= 10.minutes.ago
end

def last_request
authorization.verification_metadata["code_sent_at"]
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ en:
title: My information
enter_your_information: My information
required_fields: "* Required fields are marked with an asterisk"
school_info: Text message login successful. Please enter few more details and you are done.
school_info: Text message verification successful. Please enter few more details and you are done.
error: Failed to save the details. Please try again or contact the system administrator.
grade_help: What is your grade level at your school?
submit: Save and continue
Expand All @@ -198,7 +198,7 @@ en:
title: My information
enter_your_information: Please enter your information
required_fields: "* Required fields are marked with an asterisk"
school_info: Text message login successful. Please enter few more details and you are done.
school_info: Text message verification successful. Please enter few more details and you are done.
error: Failed to save the details. Please try again or contact the system administrator.
grade_help: What is your grade level at your school?
submit: Save and continue
Expand Down

0 comments on commit 2cdbd0a

Please sign in to comment.