devise icon indicating copy to clipboard operation
devise copied to clipboard

undefined method `new_session_path` for omniauth_callbacks controller

Open riffraff opened this issue 15 years ago • 32 comments

this error appeared in our codebase since an upgrade from 1.3.4 to 1.4.8, and it seems somewhat related to similar errors reported recently. It consistently happens whenever an authorization is denied by the user and we end up on the failure side of the omniauth controller. Our code in callbacks controller is pretty much identical to that in the docs/wiki and worked fine in the past. We also see the error when hitting the recovery password page, similarly to an already reported and closed issue.

In our case the route code looks like

devise_for :users , :controllers => { 
  :omniauth_callbacks => "omniauth_callbacks" ,
  :registrations => 'registrations',
  :sessions =>'sessions'
}

and the quick fix was to simply add

def new_session_path *args 
  new_user_session_path *args
end

to our ApplicationController

riffraff avatar Oct 17 '11 08:10 riffraff

I also have this error in my app since an upgrade to 1.4.8. It occurs whenever a user requests a password reset email or a re-send of confirmation instructions. We're using rails 3.0.3.

kevinrutherford avatar Oct 17 '11 15:10 kevinrutherford

+1 received this error as well going from 1.4.7 to 1.4.8

jdx avatar Oct 17 '11 18:10 jdx

I can confirm that downgrading to 1.4.7 fixes this issue.

Rails 3.0.3, basic Devise setup

krisleech avatar Oct 17 '11 20:10 krisleech

This should be fixed in master and v1.4 branches. Please confirm and I will gladly release 1.4.9. Thanks!

josevalim avatar Oct 18 '11 06:10 josevalim

yep seems to work for me, fixing the error both in password recovery and omniauth failure.

riffraff avatar Oct 18 '11 06:10 riffraff

Thanks, I can also confirm HEAD fixes this for Rails 3.0.3 / JRuby.

krisleech avatar Oct 18 '11 07:10 krisleech

This issue seems to have regressed in 1.5.3 with omniauth 1.0.1

patbenatar avatar Jan 20 '12 21:01 patbenatar

Yes I am getting this issue again

rebo avatar Apr 26 '12 13:04 rebo

Same, needs to be reopened

krzkrzkrz avatar May 02 '12 09:05 krzkrzkrz

Can someone provide a way to reproduce the issue? Otherwise, there is no way we can fix it.

josevalim avatar May 02 '12 10:05 josevalim

I am closing this again for the same reason: lack of ways to reproduce the issue. Thanks.

josevalim avatar May 06 '12 10:05 josevalim

I came across this issue in an app that I am migrating from ruby 1.8.7/rails 3.0.7 to 1.9.3/3.2.8. Make sure the following is not in your config/application.rb:

config.middleware.insert_before(Warden::Manager, Rack::OpenID)

jeperkins4 avatar Oct 04 '12 21:10 jeperkins4

I encountered this whilst using Devise as confirmable only. It occurs for me when a user tries to confirm an email address using a token that has expired. Clearly it's necessary to define a route for this eventuality - so just to confirm, is defining a route like:

match "a/b", :via => [:get, :put], :as => "new_session"

the 'correct' way to define the route?

myitcv avatar Feb 25 '13 00:02 myitcv

I ran into this today, but it was a narrow use case:

I'm writing an engine that uses devise version 2.2.4 plus omniauth-oauth2, and includes a custom subclass of Devise::OmniauthCallbacksController. I am only using :omniauthable in my User model.

The error only happens in the test/dummy app, which itself extends the engine's OmniauthCallbacksController (to implement a stub method), but not in a 'real' app that is using the gem in the same fashion. I'm in the process of trying to figure out if there is a notable difference in between this app and the test dummy app's configurations.

I'm running an integration test in the engine with the dummy app. I put a breakpoint in Devise.generate_helpers!, and see the new_session_path being generated, but for whatever reason, it's not available in the custom OmniauthCallbacksController subclass in the dummy app. class.ancestors in that controller does show the Devise::Controllers::UrlHelpers being included, so I must be missing something. I'll keep digging.

twelve17 avatar Jul 23 '13 17:07 twelve17

In the docs it says

If you are using ONLY omniauth authentication, you need to define a route named new_user_session (if not defined, root will be used)

but in fact it raises the aforementioned error and doesn't use the root. I'm on a brand new Rails4 app with gems

devise (3.1.0)
omniauth (1.1.4)
omniauth-linkedin-oauth2 (0.1.1)
omniauth-oauth2 (1.1.1)

RKushnir avatar Sep 22 '13 21:09 RKushnir

@josevalim, got same issue on 3.2.4. Faced this problem when omniauth provider returns error and https://github.com/plataformatec/devise/blob/master/app/controllers/devise/omniauth_callbacks_controller.rb#L28 is called.

I use only

class User < ActiveRecord::Base
  devise :omniauthable, :rememberable, :trackable
  # ...
end

so seems like new_session_path haven't been generated.

I have routes for new_user_session_path but it doesn't help:

  devise_scope :user do
    get '/session/new(.:format)'        =>  'devise/sessions#new',      as: :new_user_session
    get '/session/sign_out(.:format)'   =>  'devise/sessions#destroy',  as: :destroy_user_session
  end

This fix works for me:

class ApplicationController < ActionController::Base
  # ...
  def new_session_path(scope)
    new_user_session_path
  end
end

Should we reopen issue?

printercu avatar Apr 13 '14 07:04 printercu

It should work, because the new_session_path is generated by Devise.

You can check the code here https://github.com/plataformatec/devise/blob/b8ed2f31608eccb6df6d5bb2e66238d6dfc4bcfc/lib/devise/controllers/url_helpers.rb#L3-27 and here https://github.com/plataformatec/devise/blob/b8ed2f31608eccb6df6d5bb2e66238d6dfc4bcfc/lib/devise/controllers/url_helpers.rb#L48-51

Could you provide a sample app to reproduce this bug?

laurocaetano avatar Apr 16 '14 20:04 laurocaetano

@laurocaetano we just generate those routes depending on the devise functionality you are using:

https://github.com/plataformatec/devise/blob/b8ed2f31608eccb6df6d5bb2e66238d6dfc4bcfc/lib/devise/controllers/url_helpers.rb#L36-L39

I am not sure what is the best way to proceed here. Maybe a respond_to? check with a proper error message? Or maybe leave it as is and say new_session_path or after_omniauth_failure_path_for must be redefined. We just need to agree on something.

josevalim avatar Apr 16 '14 21:04 josevalim

Мауbe just create this helper if :omniauthable is used? There is notice in documentation that one should add new_user_session route manually if uses :omniauthable without :database_authenticatable. So the route should be present, we only need a helper.

printercu avatar Apr 17 '14 06:04 printercu

@printercu yeah, that is likely a better option!

josevalim avatar Apr 17 '14 06:04 josevalim