-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow executing code after sign in and before sign out
**Why** In some cases, it might be necessary to run some code right after the user signs in, but before the OTP is sent, and also right before a user signs out. For example, consider this scenario: - The app requires the user to confirm their phone number before it gets saved. This confirmation is done by sending an OTP to the phone and asking the user to enter it. - User mistypes the number, then closes the anonymous browser window, or signs out before confirming - User signs back in, and OTP is sent to the mistyped number. User is now unable to fully sign in since the OTP is being sent to the wrong number In order to prevent this scenario, we need to be able to reset the `unconfirmed_mobile` to nil before the OTP is sent, and before they sign out so that they can type it in again. **How** Allow the gem user to define an OtpSender class with a `reset_otp_state` method
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/two_factor_authentication/hooks/two_factor_authenticatable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,25 @@ | ||
Warden::Manager.after_authentication do |user, auth, options| | ||
reset_otp_state_for(user) | ||
|
||
if user.respond_to?(:need_two_factor_authentication?) && | ||
!auth.env["action_dispatch.cookies"].signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME] | ||
if auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) | ||
user.send_two_factor_authentication_code | ||
end | ||
end | ||
end | ||
|
||
Warden::Manager.before_logout do |user, _auth, _options| | ||
reset_otp_state_for(user) | ||
end | ||
|
||
def reset_otp_state_for(user) | ||
klass_string = "#{user.class}OtpSender" | ||
return unless Object.const_defined?(klass_string) | ||
|
||
klass = Object.const_get(klass_string) | ||
|
||
otp_sender = klass.new(user) | ||
|
||
otp_sender.reset_otp_state if otp_sender.respond_to?(:reset_otp_state) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters