janko / rodauth-rails

Rails integration for Rodauth authentication framework
https://github.com/jeremyevans/rodauth
MIT License
565 stars 40 forks source link

Issue with new rails 7 --api app and default install #160

Closed juan-apa closed 1 year ago

juan-apa commented 1 year ago

Description

I just created a new rails 7 --api app to try out rodauth auth method, and I have an issue with the default install. When trying to create an account via the api, the email fails to send with the following message:

[ActiveJob] [ActionMailer::MailDeliveryJob] [0bb1e653-8be5-4e97-a218-a44f19050efa] Performing ActionMailer::MailDeliveryJob (Job ID: 0bb1e653-8be5-4e97-a218-a44f19050efa) from Async(default) enqueued at 2022-09-19T12:15:58Z with arguments: "RodauthMailer", "verify_account", "deliver_now", {:args=>[10, "JVmxIEwhak7Ppq2v-6GVMZP86r7TAkxVpHNKnDM6umM"]}
[ActiveJob] [ActionMailer::MailDeliveryJob] [0bb1e653-8be5-4e97-a218-a44f19050efa] RodauthMailer#verify_account: processed outbound mail in 1.4ms
[ActiveJob] [ActionMailer::MailDeliveryJob] [0bb1e653-8be5-4e97-a218-a44f19050efa] Error performing ActionMailer::MailDeliveryJob (Job ID: 0bb1e653-8be5-4e97-a218-a44f19050efa) from Async(default) in 3.25ms: ArgumentError (wrong number of arguments (given 2, expected 3)):
/Users/juan/Documents/personal/autopay-api/app/mailers/rodauth_mailer.rb:2:in `verify_account'

I tracked this issue to the splat operator in RodauthMain -> configure -> create_verify_account_email. When self.class.configuration_name returns empty, the splat operator changed the arity of the method call from 3 to 2. So instead of calling:

RodauthMailer.reset_password(nil, <account_id>, <reset_password_key_value>)

it calls

RodauthMailer.reset_password(<account_id>, <reset_password_key_value>)

which fails in runtime, and does not send the email, so an account can never be verified.

Solution

The solution to this was to create a method in RodauthMain after the configure do ... end:

def configuration_name
    self.class.configuration_name.present || nil
  end

Then in the configure block, the methods that call the mailer, then call: RodauthMailer.verify_account(configuration_name, account_id, verify_account_key_value) instead of RodauthMailer.verify_account(*self.class.configuration_name, account_id, verify_account_key_value) (make this change to every mailer method called from this class.)

I'm not sure of the repercussions of removing the splat operator when multiple configurations for rodauth are setup, so this fix may not work for you.

janko commented 1 year ago

Thanks for reporting, I forgot to update the Rodauth config after making configuration name a required argument in RodauthMailer (https://github.com/janko/rodauth-rails/commit/523c2a01f7b4fa9413af66676bed410728587444). I will push a fix shortly, and probably a new release.

juan-apa commented 1 year ago

thanks for the quick reply!