I Want to Override Authenticate_User and Current_User Method of Devise Gem

I want to override authenticate_user and current_user method of devise gem

You may be able to monkey-patch it like:

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 has some customizability built into it already, and overriding these methods makes me wonder "why use Devise at all?"

Rails Michael Hartl Tutorial + Devise current_user?

Devise gives you the current_user helper by default. However, it doesn't give you current_user? boolean method. You need to define this method yourself in application_helper.rb:

def current_user?(user)
user == current_user
end

Now you have current_helper? defined you can pass in your @user instance variable

<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>

Thats all you have to do. You don't have to touch your ApplicationController or SessionsHelper in any way. You are simply using current_user which Devise gives you in order to define current_user?(user). This way is much easier to implement. Hope this helps

skip_before_filter ignores conditionals

It is a Rails bug (or at least an undocumented strange behaviour). It is tracked here: https://github.com/rails/rails/issues/9703

In this thread, you can find a (twisted) solution.

Instead of

skip_before_filter :authenticate_user!, :only => :show, :if => :in_production

write

skip_before_filter :authenticate_user!, :only => :show
before_filter :authenticate_user!, :only => :show, :unless => :in_production

It worked for me.



Related Topics



Leave a reply



Submit