Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

devise_token_auth with multiple models and auth headers

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?

like image 268
Armando Avatar asked Apr 08 '16 17:04

Armando


1 Answers

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.

like image 142
Sovalina Avatar answered Nov 04 '22 23:11

Sovalina