Devise: custom strategy returning "uninitialized constant Devise::Models::CustomStrategy"

Created on 12 May 2016  路  9Comments  路  Source: heartcombo/devise

Following the guide to adding a an ldap strategy, I am creating a new custom strategy on version 3.5.6 and I am running up against this error when spinning up rails:

uninitialized constant Devise::Models::CustomStrategy

I added an initializer file custom_strategy.rb

require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class CustomStrategy < Authenticatable
      def authenticate!
        resource  = password.present? && mapping.to.find_for_database_authentication(authentication_hash)
        encrypted = false

        if validate(resource){ encrypted = true; resource.valid_password?(password) }
          remember_me(resource)
          resource.after_database_authentication
          success!(resource)
        end

        mapping.to.new.password = password if !encrypted && Devise.paranoid
        fail(:not_found_in_database) unless resource
      end
    end
  end
end

Warden::Strategies.add :custom_strategy, Devise::Strategies::CustomStrategy

and added the default to devise.rb

  config.warden do |manager|
    manager.default_strategies(scope: :login).unshift :custom_strategy
  end

but no luck. Am I doing something wrong?

UPDATE

I updated to 4.1.0 with the same error

ANOTHER UPDATE

I removed the reference to :custom_strategy in my model login.rb so it reads:

class Login < ActiveRecord::Base
  devise :rememberable, :trackable, :validatable, :lockable, :timeoutable
end

but now I'm getting a browser error Too many redirects

Needs more info

Most helpful comment

@tegon i got it to work now:

ldap_authenticable.rb:

require 'net/ldap'
require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LdapAuthenticatable < Authenticatable
      def authenticate!
       (...)
    end
  end
end

Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)

devise.rb:

Devise.setup do |config|
  config.warden do |manager|
    manager.default_strategies(:scope => :admin_user).unshift :ldap_authenticatable
  end

without including the stategy in the model
admin_user.rb:

class AdminUser < ApplicationRecord
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable
end

All 9 comments

I'm not sure if the proper way of creating a custom strategy is extending a Devise class and using the Devise namespace. The custom strategy example extends the Warden strategy and not use the Devise namespace, take a look at this test case.

Now I see, you are following this wiki right? Well, can you provide a sample application that reproduces the error?

Right, I was following the wiki. I don't know how, but it magically started working. Not sure what did it, but it's working now! Thanks.

Welcome back, I've got the exact same issue:

11: from /home/mcdostone/X/app/models/user.rb:3:in `<main>'
10: from /home/mcdostone/X/app/models/user.rb:4:in `<class:User>'
9: from /home/mcdostone/.rvm/gems/ruby-2.5.1/gems/devise-4.5.0/lib/devise/models.rb:86:in `devise'
...
/home/mcdostone/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant': uninitialized constant Devise::Models::LdapAuthenticatable (NameError)

# config/initializers/devise.rb
config.warden do |manager|
    manager.strategies.add(:ldap_authenticatable, Devise::Models::LdapAuthenticatable)
    manager.default_strategies(scope: :user).unshift :ldap_authenticatable
  end

# app/models/user.rb
class User < ApplicationRecord
  devise :ldap_authenticatable
}
end

Any idea what is going wrong?

@Mcdostone I've got the same error, Did you find the solution?

same here

@remipou Can you open a new issue and include a sample application? That would help us find the problem.

@tegon i got it to work now:

ldap_authenticable.rb:

require 'net/ldap'
require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LdapAuthenticatable < Authenticatable
      def authenticate!
       (...)
    end
  end
end

Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)

devise.rb:

Devise.setup do |config|
  config.warden do |manager|
    manager.default_strategies(:scope => :admin_user).unshift :ldap_authenticatable
  end

without including the stategy in the model
admin_user.rb:

class AdminUser < ApplicationRecord
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable
end

@remipou That seems good to me. Anyway, if you find any problems feel free to open a new issue.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings