Speed up sign-in mail sending

This commit is contained in:
Max Wofford
2025-03-17 10:48:00 -04:00
parent 8a946926fe
commit 0a4f46283c
3 changed files with 32 additions and 18 deletions

View File

@@ -36,16 +36,13 @@ class SessionsController < ApplicationController
def email
email = params[:email].downcase
# Use a transaction to ensure both user and email are created atomically
email_address = ActiveRecord::Base.transaction do
EmailAddress.find_by(email: email) || begin
user = User.create!
user.email_addresses.create!(email: email)
end
if Rails.env.production?
HandleEmailSigninJob.perform_later(email)
else
HandleEmailSigninJob.perform_now(email)
end
LoopsMailer.sign_in_email(email_address).deliver_later
redirect_to root_path(sign_in_email: true)
redirect_to root_path(sign_in_email: true), notice: "Check your email for a sign-in link!"
end
def token

View File

@@ -0,0 +1,14 @@
class HandleEmailSigninJob < ApplicationJob
queue_as :default
def perform(email)
email_address = ActiveRecord::Base.transaction do
EmailAddress.find_by(email: email) || begin
user = User.create!
user.email_addresses.create!(email: email)
end
end
LoopsMailer.sign_in_email(email_address).deliver_now
end
end

View File

@@ -1,14 +1,17 @@
class LoopsMailer < ApplicationMailer
# Override the default mailer settings to use Loops.so SMTP
self.delivery_method = :smtp
self.smtp_settings = {
address: "smtp.loops.so",
port: 587,
user_name: "loops",
password: ENV["LOOPS_API_KEY"],
authentication: "plain",
enable_starttls: true
}
if Rails.env.development? && ENV["LOOPS_API_KEY"].nil?
self.delivery_method = :letter_opener
else
self.delivery_method = :smtp
self.smtp_settings = {
address: "smtp.loops.so",
port: 587,
user_name: "loops",
password: ENV["LOOPS_API_KEY"],
authentication: "plain",
enable_starttls: true
}
end
def sign_in_email(email_address)
@email = email_address.email