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

Unable to override sessions controller #471

Closed
dgoradia opened this issue Dec 14, 2015 · 14 comments
Closed

Unable to override sessions controller #471

dgoradia opened this issue Dec 14, 2015 · 14 comments

Comments

@dgoradia
Copy link

Or any controller. I really just need to override the render methods.

Following from the readme under https://github.com/lynndylanhurley/devise_token_auth#custom-controller-overrides, it just isn't working for me.

Rails.application.routes.draw do

  mount_devise_token_auth_for 'User', at: 'auth', skip: [:sessions, :registrations, :passwords, :omniauth_callbacks]

  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
    scope module: :v1 do

      # devise_for :users
      mount_devise_token_auth_for 'User', at: 'auth', skip: [:token_validations],
      controller: {
        sessions: 'devise_token_auth/sessions'
      }
      ...

app/controllers/api/v1/devise_token_auth/sessions_controller.rb

  class Api::V1::SessionsController < DeviseTokenAuth::SessionsController

    protected

    def render_create_success
      data = @resource.token_validation_response

      role = Role.find(@resource.id)
      data[:role] = role.name
      render json: {
        data: data
      }
    end

  end

It doesn't use this controller and just returns the default. Any suggestions would be appreciated!

@proxygear
Copy link

Get a similar problem.
The call to auth/sign_in return the desired JSON ( I checked with the browser inspector).

However :
$scope.send = ()->
$auth.submitLogin($scope.user)
.then (response)-> console.log "response", response

The logged response object contains the default data ...

@dgoradia
Copy link
Author

I was able to workaround this by overriding the token_validation_response method in the model since I'm using active_model_serializers:

  def token_validation_response
    UserSerializer.new( self, root: false )
  end

and adding the uid and provider back using my serializer:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :provider, :uid, :name, :nickname, :image, :email, :created_at, :updated_at
  has_one :role
end

...but the issue still stands. If I wanted to override any of the controllers, it's still not working.

@essn
Copy link

essn commented Dec 17, 2015

I have this problem with SessionsController as well, I've been able to override Registrations and OmniAuthCallbacks no problem. Been using the same workaround as @dgoradia to serialize the validation response but now I need to get inside the SessionsController#new method, any insight is greatly appreciated.

@llaine
Copy link

llaine commented Jan 2, 2016

+1

@tbloncar
Copy link
Contributor

@dgoradia In your example, you're keying the overrides hash with controller rather than controllers. Maybe this is just a typo, as it seems that others are having issues with this, as well; just wanted to point that out to be sure.

@vr4b4c
Copy link

vr4b4c commented Feb 28, 2016

Define full module nesting to overriding controller

# confg/routes.rb
mount_devise_token_auth_for 'User', at: 'auth', skip: [:token_validations],
  controllers: {
    sessions: 'api/v1/devise_token_auth/sessions' # full module nesting
  }

Define controller in specified file system nesting(to be picked up by autoloading system)

# app/controllers/api/v1/devise_token_auth/sessions_controller.rb
module Api
  module V1
    module DeviseTokenAuth
      class SessionsController < ::DeviseTokenAuth::SessionsController
        # note :: for loading root constants(::DeviseTokenAuth::SessionsController)
        # otherwise you'll get circular dependency error
        protected
        # custom rendering
      end
    end
  end
end

@kaiomagalhaes
Copy link

@vr4b4c the right would be:

mount_devise_token_auth_for 'User', at: 'auth', skip: [:token_validations],
  controllers: {
    sessions: 'api/v1/devise_token_auth/sessions' # full module nesting
  }

It is controller_S_ and not controller.

Am I wrong?

P.s: Thanks for your answer

@vr4b4c
Copy link

vr4b4c commented Mar 1, 2016

@kaiomagalhaes you're correct(edited my comment)

@kaiomagalhaes
Copy link

@vr4b4c and it should be:

module Api
  module V1
    module DeviseTokenAuth

@vr4b4c
Copy link

vr4b4c commented Mar 1, 2016

true, apparently my solution has more problems than original issue... ;)

@albertsaave
Copy link

albertsaave commented Mar 22, 2017

While implementing what @vr4b4c suggested and writing tests to my api controllers, I now get application controller token uninitialized constant Concerns::SetUserByToken in my tests only. Previously, my tests were passing.

Any thoughts?

@zachfeldman
Copy link
Contributor

Hi there @dgoradia and @albertsaave ,

In an effort to cleanup this project and prioritize a bit, we're marking issues that haven't had any activity in a while with a "close-in-7-days" label. If we don't hear from you in about a week, we'll be closing this issue. Obviously feel free to re-open it at any time if it's the right time or this was done in error!

If you are still having the issue (especially if it's a bug report) please refer to our new Issue Template to provide some more details to help us solve it.

Hope all is well.

@CostaRico
Copy link

also cant override :(

@zachfeldman
Copy link
Contributor

Hi @CostaRico, this issue is closed. If you're having a similar problem, please open a new issue with more specifics and we can try to look into it.

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

No branches or pull requests

10 participants