I’m working with a Rails API backend and a React frontend app. My Rails app sends out verification emails with confirmation links.
I’m trying to figure out the best way to handle these email links. Should they go straight to the backend API or load the frontend first?
Here are the two options I’m considering:
Option A - Direct backend hit
Email link goes to api.mysite.com/verify/:token which processes the request and redirects to app.mysite.com/verification/success or /error
Option B - Frontend first
Email link opens app.mysite.com/verify/:token which then makes an AJAX call to the backend API
Which approach do you use in your projects?
For option A, I’ve set up a basic controller to handle redirects:
namespace :email_handlers do
resources :verifications
end
namespace :app_routes do
app_url = Rails.configuration.app_domain
scope app_url do
resources :verifications, only: [] do
get 'success'
get 'error'
end
end
end
And my verification controller looks like:
class EmailHandlers::VerificationsController
def show
service = VerifyTokenService.new(token: params[:token])
if service.process
redirect_to(
app_success_verification_url,
user_email: service.user.email
)
else
redirect_to(app_error_verification_url)
end
end
end
I’m having trouble with the URL helpers when pointing to different domains. What’s the right way to handle cross-domain redirects here? Also, what HTTP status codes should I use for success vs failure redirects?
Both approaches seem to need a lot of coordination between frontend and backend so I want to make sure I pick the right one.