devise icon indicating copy to clipboard operation
devise copied to clipboard

rails 5 authenticate_user! has not been define

Open atstockland opened this issue 10 years ago • 16 comments

I apologize if this is not a devise bug. My development env was working great with passing tests. But, when I pushed to production on heroku my app crashed with the following error.

Unable to load application: ArgumentError: Before process_action callback :authenticate_user! has not been define.

My routes files includes devise_for :users and I did restart my heroku dynos. I ended up changing config.eager_load = false in production and my app came back to life. I'm still on the hunt for the source and the solution.

atstockland avatar Jul 20 '16 04:07 atstockland

Versions for rails and devise?

I'm using rails and devise on heroku and it works well.

leonardoprg avatar Jul 22 '16 10:07 leonardoprg

Rials 5.0.0 and device 4.2.0

atstockland avatar Jul 22 '16 14:07 atstockland

weird, same versions here,

just to confirm, did you ran the steps on Readme ?

for me just ran the steps, send to heroku and it works.

leonardoprg avatar Jul 22 '16 14:07 leonardoprg

I did, but I am going to double check through the readme.

atstockland avatar Jul 22 '16 14:07 atstockland

other quick question, did you ran rails db:migrate on heroku?

I asked because when doesnt run, heroku can throw some weird exceptions like it.

leonardoprg avatar Jul 22 '16 14:07 leonardoprg

I did migrate. I changed config.eager_load to true in development and I get the exact same error. It doesn't occur on page load, it occurs when the server is trying to start.

/Users/LilWayne/.rvm/gems/ruby-2.3.1@rails_4/gems/activesupport-5.0.0/lib/active_support/callbacks.rb:641:in `block (2 levels) in skip_callback': Before process_action callback :authenticate_user! has not been defined (ArgumentError)

So, the error is occurring when calling "skip_before_action :authenticate_user!". I'm still not sure if this is a Devise bug or an implementation issue.

atstockland avatar Jul 22 '16 15:07 atstockland

Ok, yeah, what you can do is create an other superclass of ApplicationController that doesnt contains the authenticate_user!.

or just skip it with an unless: :devise_controller? for example.

It's a possible solution, but how I told, it works for me on the same environment.

leonardoprg avatar Jul 22 '16 15:07 leonardoprg

It seems this is a Rails 5 feature. Rails 5 will throw an error if the method you are trying to skip is not defined. Sorry, for treating Devise issue like StackOverFlow. But, I honestly thought it was a Devise method bug.

skip_before_action :authenticate_user!, unless: :devise_controller? still results in the same error. It also affects the Pundit gem when calling "skip_after_action :verify_authorized".

I'm sure there is a rails solution. I'm gonna close this and move over to StackOverFlow with this.

Thank you @leonardoprg !

atstockland avatar Jul 22 '16 15:07 atstockland

In case someone else ends up here thinking this is related to Devise here is the answer: (this error has nothing to do with any gems...it is due to changes in the skip_xxx_action in rails 5)

Simply change ---

skip_before_action :authenticate_user!

to

skip_before_action :authenticate_user!, raise: false

https://github.com/thoughtbot/clearance/issues/621

atstockland avatar Jul 22 '16 16:07 atstockland

atstockland: nope that doesn't work because the skip_before_action won't work, and that brings back the original problem, too many redirects...

RichardGrossman avatar Nov 20 '16 21:11 RichardGrossman

I'm having a similar issue with Rails 5 and Devise 4.2.0. before_action :authenticate_user! returns: Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) Sorry for commenting here - This is the only place I could find any mention of the problem :/

gwalshington avatar Jan 04 '17 16:01 gwalshington

@gwalshington - I tried to move this over to SO, but that proved a pain. People wanted to know the history of the question w/o referring to this issue. It's been a while since I addressed this issue, so I can't quit remember what I did. The answer I proved above is no longer valid and it appears that code has been removed from my app. Here is some relevant code from my application_controller---I hope it helps.

after_action :verify_authorized, except: :index, unless: :devise_controller? # pundit
after_action :verify_policy_scoped, only: :index, unless: :devise_controller? # pundit
before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  added_attrs = [:username, :email, :password, :password_confirmation, :remember_me, :current_password]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs

end

atstockland avatar Jan 04 '17 20:01 atstockland

@atstockland thank you!

gwalshington avatar Jan 04 '17 21:01 gwalshington

@atstockland Do you mind linking your SO thread here?

mxmzb avatar Jan 19 '17 21:01 mxmzb

Here's an SO question discussing this: http://stackoverflow.com/questions/35394261/after-updating-to-rails-5-and-devise-4-0-0-rc1-the-devise-authenticate-user-me

tarr11 avatar Mar 30 '17 16:03 tarr11

in my case the below code worked out!! before_action :authenticate_user!

class ApplicationController < ActionController::API

  include Devise::Controllers::Helpers

  before_action :authenticate_user!

rray524 avatar Nov 14 '24 23:11 rray524