Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omniauth callback redirect not working properly when using namespace/scope #362

Closed
abhayastudios opened this issue Aug 29, 2015 · 12 comments

Comments

@abhayastudios
Copy link

In 0.1.34 I found that the callback redirect does not work with this routes definition:

devise_for :users
namespace :api do
  scope :v1 do
    mount_devise_token_auth_for 'User', at: 'auth'
  end
end

Instead of redirecting to /api/v1/auth/facebook/callback, it will try to redirect to /users/facebook/callback which does not exist. So I figured this is a problem because I am trying to use both devise and devise_token_auth in parallel, but even without the devise_for (and nothing else in the routes) it does not work and it dies on on some NilClass error:

2015-08-29T08:39:21.190658+00:00 app[web.1]: Started GET "/omniauth/facebook/callback?code=<code>&state=<state>" for <IP> at 2015-08-29 08:39:21 +0000
2015-08-29T08:39:21.192644+00:00 app[web.1]: I, [2015-08-29T08:39:21.192457 #3]  INFO -- omniauth: (facebook) Callback phase initiated.
2015-08-29T08:39:22.198525+00:00 app[web.1]: Processing by DeviseTokenAuth::OmniauthCallbacksController#redirect_callbacks as HTML
2015-08-29T08:39:22.198562+00:00 app[web.1]:   Parameters: {"code"=>"<code>", "state"=>"<state>", "provider"=>"facebook"}
2015-08-29T08:39:22.199647+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms
2015-08-29T08:39:22.201301+00:00 app[web.1]:
2015-08-29T08:39:22.201306+00:00 app[web.1]:   app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:15:in `redirect_callbacks'
2015-08-29T08:39:22.201304+00:00 app[web.1]: NoMethodError (undefined method `[]' for nil:NilClass):

This undefined method seems to be related to Devise.mappings[:users] being nil, whereas Devise.mappings[:api_user] is working.

As far as I can tell it is because of this code in the devise_token_auth application_controller.rb, where in my case it apparently falls back to the Devise.mappings.values.first but I don't really have enough of an understanding of the gem to tell how it should be improved:

def resource_class(m=nil)
      if m
        mapping = Devise.mappings[m]
      else
        mapping = Devise.mappings[resource_name] || Devise.mappings.values.first
      end

      mapping.to
    end
end

As an ugly workaround, I thought to overwrite the commented out line in the redirect_callbacks method in omniauth_callbacks_controller.rb by the one below it:

#devise_mapping = request.env['omniauth.params']['resource_class'].underscore.to_sym
devise_mapping = 'api_user'.underscore.to_sym

That doesn't work either because now the value of redirect_route is /auth/facebook/callback instead of /api/v1/auth/facebook/callback. So I ended up doing it like this:

#redirect_route = "/#{Devise.mappings[devise_mapping].as_json["path"]}/#{params[:provider]}/callback"
redirect_route = "/api/v1/auth/#{params[:provider]}/callback"

Of course this works for me because I don't have multiple models, but it seems to me that this namespace/scope structure should be automatically detected and the appropriate route be used for the callback redirect.

Thanks!

@abhayastudios
Copy link
Author

Related to issue #166

@jeffects
Copy link

jeffects commented Sep 3, 2015

Same issue here

@Jellyfishboy
Copy link

Same issue here!

@romankovt
Copy link
Contributor

absolutely the same issue

@ACPK
Copy link

ACPK commented Oct 12, 2015

Seem to be having the same issue too. When using FB oauth, I get redirected to:

http://api/v1/auth/facebook/callback#_=_

@mkhatib
Copy link

mkhatib commented Nov 1, 2015

Same here. FB and Google+ login is broken on manshar.com. Investigating what's going on.

@charlesdg
Copy link
Contributor

same problem here,

as I have only one model concerned, I override the redirect_callbacks method and change the redirect_route variable with
redirect_route = "/api/auth/#{params[:provider]}/callback"

devilankur18 added a commit to devilankur18/devise_token_auth that referenced this issue Dec 18, 2015
devilankur18 added a commit to devilankur18/devise_token_auth that referenced this issue Dec 18, 2015
Fixes Issue lynndylanhurley#362: Fixes for the omniauth redirection issue for namesp…
booleanbetrayal added a commit that referenced this issue Dec 30, 2015
Fixes Issue #362: Fixes for the omniauth redirection issue for namesp…
@booleanbetrayal
Copy link
Collaborator

Should be fixed via #476 . Closing but can re-open as needed.

@charlesdg
Copy link
Contributor

not working at my side:

undefined method 'fullpath' for nil:NilClass

on this line in app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:

redirect_route = "#{request.protocol}#{request.host_with_port}/#{Devise.mappings[devise_mapping].fullpath}/#{params[:provider]}/callback"

@magnumfonseca
Copy link

same issue

devise_mapping => :user
Devise.mappings[devise_mapping] => nil

@ivawzh
Copy link

ivawzh commented Mar 14, 2016

Same issue described by @magnumfonseca

devise_mapping => :user
Devise.mappings[devise_mapping] => nil

@booleanbetrayal
Copy link
Collaborator

Should be fixed now as of #540

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants