ruby-on-railsdevisesingle-page-application

Rails 4.2.6 - Devise 4.2.2 - Default Auth


I am looking to set a secondary password by which I can authenticate a user for a login as from admin. The reason for this work around is the front end is a single page application.

Each user has been given a unique login_as string. now I need to configure Devise to compare the login_as if the password fails.

Any help is appreciated. I am of course open to an alternative solution if there is a better way.

Thanks.


Solution

  • This [post][1] from Duncan Robertson was very helpful in solving my issue. I essentially created an override strategy and called it in the devise.rb file. I had some concern regarding tampering with a large user base but it has proved successful. By adding a column to users named ":signin_as" and then setting it to a default unique string with a rake I then had what I needed to fallback on if the initial sign in failed.

    the override strategy (config/initializers/auth_override.rb)

    module Devise
      module Strategies
        class AuthOverride < Authenticatable
          def custom_auth(user, signin_as)
            user[:signin_as] == signin_as
          end
    
          def authenticate!
            user = User.find_by_email(email)
            
            unless user fail
            
            if user.valid_password?(params[:password]) || custom_auth(user, params[:password])
              success!(user)
            else        
              fail
            end
          end
        end
      end
    end
    

    including the strategy in devise (config/initializers/devise.rb)

    config.warden do |manager|
      manager.default_strategies(:scope => :user).unshift :auth_override
    end
    

    blog on this