Skip to content

Commit

Permalink
Update email auth to use a common OnRuby sender (#1079)
Browse files Browse the repository at this point in the history
This way, we don't need to add each RUG's email address to the
Sendgrid list of authorized senders
  • Loading branch information
josepegea authored Nov 5, 2024
1 parent bbd7870 commit 0410882
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions app/controllers/concerns/with_email_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ def email_login
email = normalize_email(params[:email])
if email.present? && valid_looking_email?(email)
token = EmailAuthToken.generate(email)
from = Whitelabel[:email]
label_name = t("label.#{Whitelabel[:label_id]}.name")
label_link = Whitelabel[:canonical_url]
UserMailer.login_link(email, token, from, I18n.locale,
UserMailer.login_link(email, token, I18n.locale,
label_name, label_link).deliver_later

redirect_to root_path, notice: t('email_auth.email_sent', email:)
Expand Down
8 changes: 5 additions & 3 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# Original author: https://github.com/phoet

class UserMailer < ApplicationMailer
def login_link(email, token, from, locale, label_name, label_link) # rubocop:disable Metrics/ParameterLists
COMMON_SENDER = 'global@onruby.de'

def login_link(email, token, locale, label_name, label_link)
@token = token
@from = from
@label_name = label_name
@label_link = label_link

I18n.with_locale(locale) do
mail from: @from, to: email, subject: t('email_auth.subject', label: label_name)
mail from: COMMON_SENDER, to: email,
subject: t('email_auth.subject', label: label_name)
end
end
end
5 changes: 2 additions & 3 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
describe UserMailer do
describe '#login_link' do
subject(:mail) do
described_class.login_link(email, token, from, locale, label_name, label_link).deliver_now
described_class.login_link(email, token, locale, label_name, label_link).deliver_now
end

let(:email) { 'user@example.com' }
let(:token) { '12345678' }
let(:from) { 'no-reply@example.com' }
let(:locale) { :en }
let(:label_name) { 'My RUG' }
let(:label_link) { 'http://rug.org' }

it 'renders the headers' do
expect(mail.subject).to eq('Login to My RUG')
expect(mail.to).to eq([email])
expect(mail.from).to eq([from])
expect(mail.from).to eq([UserMailer::COMMON_SENDER])
end

it 'renders the body' do
Expand Down

0 comments on commit 0410882

Please sign in to comment.