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

Problem in validate_token if the model is in a namespace #69

Closed
ybian opened this issue Nov 8, 2014 · 6 comments
Closed

Problem in validate_token if the model is in a namespace #69

ybian opened this issue Nov 8, 2014 · 6 comments

Comments

@ybian
Copy link

ybian commented Nov 8, 2014

TestCase:

  1. Setup ng-token-auth and devise_token_auth_demo properly and make it work correctly;
  2. Move the model User to a namespace, say, Kuai::User, and change routes.rb accordingly to:
    mount_devise_token_auth_for 'Kuai::User', at: '/auth'
  3. Test again with registration. Confirmation email was sent successfully.
  4. Click the confirmation link in email.

Expected Result:

Should confirm successfully.

Actual Result:

It seems confirmed. But immediately got the following exception:

Started OPTIONS "/auth/validate_token" for 127.0.0.1 at 2014-11-08 08:17:08 +0800


Started GET "/auth/validate_token" for 127.0.0.1 at 2014-11-08 08:17:09 +0800
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as HTML
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `name' for nil:NilClass):
  app/controllers/application_controller.rb:9:in `configure_permitted_parameters'

The error seems in the following code snippets of lib/devise_token_auth/rails/routes.rb:

        devise_for base_name.pluralize.underscore.to_sym,
          :class_name  => resource,
          :module      => :devise,
          :path        => "",
          :controllers => controllers

if I changed it to:

        base_name = resource.split('::').last
        devise_for base_name.pluralize.underscore.to_sym,
          :class_name  => resource,
          :module      => :devise,
          :path        => "",
          :controllers => controllers

It seemed to work but I'm not sure whether this is the right solution... Please take a look.

@lynndylanhurley
Copy link
Owner

Thanks @ybian! I'll create a test case for this scenario.

ybian added a commit to ybian/devise_token_auth that referenced this issue Nov 8, 2014
@lynndylanhurley
Copy link
Owner

The fix for this has been pushed into version 0.1.30.beta4. Please test to confirm.

@mkralla11
Copy link

I know this issue is old but still exists/reappears in 0.1.32.beta5 (could be other newer versions as well). I believe its more of an issue with how devise handles the key mapping with Devise.mappings[devise_mapping] for namespaced models. Here is an example case that happens:
resource name is:
Sassed::User

devise's mapping key produces:
:sassed/user
devise_token_auth's mapping key produces:
:sassed_user

#routes.rb
mount_devise_token_auth_for 'Sassed::User', at: 'auth'
module DeviseTokenAuth
  class OmniauthCallbacksController < DeviseTokenAuth::ApplicationController
...
          def redirect_callbacks
            # The line below from original source code does not reflect
            # the correct key value set by devise
            # devise_mapping = request.env['omniauth.params']['resource_class'].underscore.to_sym
            # producing, for example: :sassed/user

            # A quick fix to make the keys match between devise and devise_token_auth
            # is shown below by replacing the / with an underscore:
            devise_mapping = request.env['omniauth.params']['resource_class'].underscore.gsub("/", "_").to_sym
            redirect_route = "/#{Devise.mappings[devise_mapping].as_json["path"]}/#{params[:provider]}/callback"

            # preserve omniauth info for success route
            session['dta.omniauth.auth'] = request.env['omniauth.auth'].except(:extra)
            session['dta.omniauth.params'] = request.env['omniauth.params']

            redirect_to redirect_route
          end
...
end

Let me know if you need more information. Nice work on the gem!

@c0mrade
Copy link

c0mrade commented Mar 6, 2015

I think this might be related with #166

m0to pushed a commit to m0to/devise_token_auth that referenced this issue Mar 19, 2015
@joeesteves
Copy link
Contributor

Hello, @mkralla11 @lynndylanhurley ,
I have the same problem using a namespace model called Ba::Usuarios (spanish word for Users and Ba:: is because it's in a module Ba:: = Base). The devise_mapping variable was set to ba/usuario when it should be ba_usuario.
i' ve soved by adding ** .gsub('/','_') ** method to the codeline it's seems to be working.

is there a correct form of doing this ?

def redirect_callbacks

  devise_mapping = request.env['omniauth.params']['resource_class'].underscore.gsub('/','_').to_sym
  redirect_route = "/#{Devise.mappings[devise_mapping].as_json["path"]}/#{params[:provider]}/callback"

my routes.rb file

Rails.application.routes.draw do

mount_devise_token_auth_for 'Ba::Usuario', at: 'auth'

namespace :ba do
    resources :usuarios
end
end

thank you,
Excelent gem!

@joeesteves
Copy link
Contributor

This issue (#355) "using namespaces in the user module" can also be solved by the previous answer!

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

5 participants