I want to override authenticate_user and current_user method for gem development

I want to override authenticate_user! and the current_user method for creating gem in my Controller application, can you please help me regarding this Thanks

+7
source share
4 answers

Perhaps you can neutralize it:

module Devise module Controllers module Helpers def authenticate_user! #do some stuff end end end end 

But I would ask what the ultimate goal is, because Devise already has customizability built into it, and overriding these methods makes me wonder, “Why use Devise at all?”

+5
source

When overriding user authentication:

Developer uses Worden under the hood https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb

So, you can simply add a new strategy to Warden to authenticate your users. See https://github.com/hassox/warden/wiki/Strategies

You do not need to override current_user. What problems do you face? Do you need another model?

+7
source

You need to create your own class to override the default behavior of Devise:

  class CustomFailure < Devise::FailureApp def redirect_url #return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope new_user_session_url(:subdomain => 'secure') end # You need to override respond to eliminate recall def respond if http_auth? http_auth else redirect end end end 

And in your config / initializers / devise.rb:

  config.warden do |manager| manager.failure_app = CustomFailure end 

But I suggest checking out the Devise documentation :)

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

+4
source

If you want to add code to authenticate_user!

 class DuckController < ApplicationController before_action :authenticate_duck ... private def authenticate_duck #use Devise method authenticate_user! #add your own stuff unless current_user.duck.approved? flash[:alert] = "Your duck is still pending. Please contact support for support." redirect_to :back end end end 
+3
source

All Articles