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

Unknown email sender config #225

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Next Next commit
Add Unknown Address mailer to default mailers
Currently, there is no configuration option to allow for sending an email when the resource is not found when the user opts into the "paranoid" option.

With this addition, users will have a special email sent to them when their email is not found in the database
  • Loading branch information
Dakota-Schramm committed May 3, 2024
commit 6f442d46691beee4da19e2c99b6fbd7c1f821af4
6 changes: 3 additions & 3 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ def handle_resource_not_found
end

def call_after_session_save
return if @skip_after_session_save_callback

if Passwordless.config.after_session_save.arity == 2
if @skip_after_session_save_callback
Passwordless.config.after_session_paranoid.call(@session, request)
elsif Passwordless.config.after_session_save.arity == 2
Passwordless.config.after_session_save.call(@session, request)
else
Passwordless.config.after_session_save.call(@session)
Expand Down
13 changes: 13 additions & 0 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,18 @@ def sign_in(session, token = nil, url_options = {})
subject: I18n.t("passwordless.mailer.sign_in.subject")
)
end

# sends an email when user attempts to login with unknown address
#
# @param session [Session] An instance of Passwordless::Session
def unknown_address(session)
email_field = session.authenticatable.class.passwordless_email_field
@email = session.authenticatable.send(email_field)

mail(
to: @email,
subject: I18n.t("passwordless.mailer.unknown_address.subject")
)
end
end
end
1 change: 1 addition & 0 deletions app/views/passwordless/mailer/unknown_address.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= t("passwordless.mailer.unknown_address.body", email: @email ) %>
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ en:

Alternatively you can use this link to sign in directly:
%{magic_link}
unknown_address:
subject: "Not Registered"
body: |-
We noticed a login attempt using your email, %{email}.

If you're seeing this email, that means that you don't currently have an
account associated with this email for Chuck.
Maybe you have a different account that's associated with your Chuck user?

If this wasn't you, disregard this email.
6 changes: 6 additions & 0 deletions lib/passwordless/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class Configuration
Mailer.sign_in(session, session.token).deliver_now
end
)
option(
:after_session_paranoid,
default: lambda do |session, _request|
Mailer.unknown_address(session).deliver_now
end
)

option :paranoid, default: false

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/passwordless/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ class << User
post("/users/sign_in", params: {passwordless: {email: "a@a"}})
end

assert_equal 1, ActionMailer::Base.deliveries.size
assert_equal 302, status

assert_equal 0, ActionMailer::Base.deliveries.size
assert_nil Session.last.authenticatable

follow_redirect!
Expand Down