Nirmalya Ghosh
Nirmalya Ghosh

Reputation: 2530

Change response required for Rails authentication

I've a user model and a login route. Using ember-simple-auth, whenever I'm sending the data for authentication, the data is required to be in this format:

{"password"=>"[FILTERED]", "email"=>"[email protected]"}

However, I want the data to be in this format:

{"user"=>{"password"=>"[FILTERED]", "email"=>"[email protected]"}}

How can I change this? Thanks. Code: https://github.com/ghoshnirmalya/hub-server

Upvotes: 0

Views: 79

Answers (1)

retgoat
retgoat

Reputation: 2464

As a possible approach you can override resoure_params method

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

  private

  def resource_params
    params.permit(user: params_for_resource(:sign_in))[:user]
  end
end

routes

namespace "api" do
  namespace "v1" do
    mount_devise_token_auth_for 'User', at: 'auth', controllers: {
      sessions:  'api/v1/sessions'
    }
    resources :users
  end
end

Then if your params looks like

{ 
    "user": {
        "email": "[email protected]",
        "password":"password"
    }
}

you will receive success response:

{
    "data": {
        "id": 1,
        "provider": "email",
        "uid": "[email protected]",
        "name": "Nirmalya Ghosh",
        "nickname": null,
        "image": null,
        "email": "[email protected]"
    }
}

But I'm strongly recommend to refactor your frontend to meet devise_token_auth reqirements and pass params as {email: "[email protected]", password: "password"}

Upvotes: 1

Related Questions