sorcery: Master version fails with "To use reset_password submodule, you must define a mailer"

Configuration

  • Sorcery Version: c30cefa7
  • Ruby Version: 2.5.1
  • Framework: Rails, versions 6.0.2.2, 5.2.4
  • Platform: Mac OS Mojave 10.14.6

Expected Behavior

No error To use reset_password submodule, you must define a mailer in the rails development console

Actual Behavior

Any operation in the rails development console fails with

validate_mailer_defined': To use reset_password submodule, you must define a mailer (config.reset_password_mailer = YourMailerClass). (ArgumentError)

Steps to Reproduce

There is code snippet that reproduces the issue:

# issue_with_sorcery.rb
require "bundler/inline"

gemfile(false) do
  gem 'rails', '6.0.2.2'
  gem 'sorcery', github: 'Sorcery/sorcery', branch: 'master', require: false
  gem 'sqlite3', '1.4.2'
end

require 'rails/all'
require 'sorcery'
module TestApp
  class Application < Rails::Application
    config.load_defaults 6.0
  end
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
  create_table "users", force: :cascade do |t|
    t.string "email", null: false
    t.string "name"
  end
end

Rails.application.config.sorcery.submodules = [:reset_password]

class UserMailer < ActionMailer::Base
  def reset_password_email(user)
    mail(to: user.email, subject: 'Welcome') do |f|
      f.html { render plain: "Hi Dear #{user.name}" }
    end
  end
end

Rails.application.config.sorcery.configure do |config|
  config.user_config do |user|
    user.reset_password_mailer = UserMailer
    config.user_class = "User"
  end
end

class User < ActiveRecord::Base
  authenticates_with_sorcery!
end

u = User.create(email: 'jane@example.org', name: 'Jane')
puts u.attributes

It is expected that this script will print user attribute values when you run it with ruby issue_with_sorcery.rb but it fails with

~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/sorcery-c30cefa751c3/lib/sorcery/model/submodules/reset_password.rb:77:in `validate_mailer_defined': To use reset_password submodule, you must define a mailer (config.reset_password_mailer = YourMailerClass). (ArgumentError)

I started seeing this error after I upgraded to the master branch to fix deprecation warnings in Rails 6

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 21 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@athix absolutely. I’m actually in the middle of completely removing Sorcery and the UserMailer. I’m going drop and re-create my database, reinstall Sorcery, and see if the issue persists. If so, I’ll upload the config file. If this fixes it, I’ll let you know as well.

@athix looks like I didn’t answer your question. 0.15 does fix the issue for me, thanks. Don’t know why @revickulous2001 still has problems.

@athix Looks like this error occurs due to load order.

Loading User class in the development console causes authenticates_with_sorcery! to be called. I inspected and found out that at this stage ::Sorcery::Controller::Config.user_config is nil. This happens because ActionController::Base has not been loaded yet and thus Sorcery :: Controller.included has not been called. And thus Config.configure! has not been called. And therefore the configuration block in the sorcery initializer has not been evaluated.

If I type the rails console ActionController::Base and then User all works OK.

Is there any reason you save sorcery config block in a variable and then evaluate it when ActionController::Base is loaded? Why not set all configuration options when sorcery initializer file evaluates?