devise_token_auth with multiple models and auth headers
Asked Answered
S

1

11

This is my problem, I override the controllers for an User model:

mount_devise_token_auth_for 'User', at: 'auth', controllers: {
     registrations:      'v1/authentication/registrations'
     sessions:           'v1/authentication/sessions'
     token_validations:  'v1/authentication/token_validations'
 }

This work well, no problems. But, when i add a new Model using the same controllers:

mount_devise_token_auth_for 'Admin', 'admin_auth', controllers: {
   sessions:           'v1/authentication/sessions',
   token_validations:  'v1/authentication/token_validations'
}
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
   registrations:      'v1/authentication/registrations',
   sessions:           'v1/authentication/sessions',
   token_validations:  'v1/authentication/token_validations'
}

Them the response header for Admin model dont have the auth keys. The response is ok(200) but dont return the auth headers. But if remove the controllers part(the override) for the Admin model the response return the auth keys. By the way, the overrides only change the render methods of the controllers. Any can help to find the way to solve this?

Sidesaddle answered 8/4, 2016 at 17:35 Comment(1)
Do you have :confirmable set for Admin? If so,Dragon
U
8

For information, I found the solution here and it works.

We need to override the devise token auth controllers for the second model and scope the success response.

For example, if the second user is a Customer:

#routes.rb
mount_devise_token_auth_for 'Customer', at: 'customer_auth', controllers: {
  sessions: 'api/v1/customer_auth/sessions',
  registrations: 'api/v1/customer_auth/registrations'
}
# controllers
class Api::V1::CustomerAuth::SessionsController < DeviseTokenAuth::SessionsController
  protected

  def render_create_success
    render json: {
      data: resource_data(resource_json: @resource.token_validation_response)
    }, scope: current_customer
  end
end

class Api::V1::Sps::CustomerAuth::RegistrationsController < DeviseTokenAuth::RegistrationsController
  protected

  def render_create_success
    render json: {
      status: 'success',
      data: resource_data
    }, scope: current_customer
  end
end

In fact the documentation speaks about it but I think it's not very explicit.

Uuge answered 16/10, 2019 at 17:4 Comment(2)
that worked for me, thank you! That's pretty bad that's not explicitly stated in the docs thoughEady
This should be added into the docs, or even better - fixing DTA so that there's no need to go through overriding existing controllers to implement multiple models.Suffocate

© 2022 - 2024 — McMap. All rights reserved.