devise icon indicating copy to clipboard operation
devise copied to clipboard

Can't customize params to be jsonapi compliant

Open Jascha-Sundaresan opened this issue 9 years ago • 6 comments

I want to have a jsonapi compliant way to log in.

My payload to the devise/sessions#create action is:

{"data"=>{"type"=>"sessions", "attributes"=>{"email"=>"[email protected]", "password"=>"123456"}}, "subdomain"=>"api", "format"=>:json, "controller"=>"devise/sessions", "action"=>"create", "session"=>{"data"=>{"type"=>"sessions", "attributes"=>{"email"=>"[email protected]", "password"=>"123456"}}}}

the authenticatable base strategy is not valid because there is no params[scope] since scope is user.

Why does the authenticatable strategy use the params object and not the devise_param_sanitizer?

I did this and it ought to work:

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_in) do |user_params|
    x = params.require(:data).permit(:type, { attributes: [:email, :password] })
    x[:attributes]
  end
end

Jascha-Sundaresan avatar Jul 25 '17 06:07 Jascha-Sundaresan

I'm trying to do the same. Any solution?

alejandrodevs avatar Jan 22 '18 16:01 alejandrodevs

The only way i found to solve this was to override all the resource_params methods (also sing_up_params, sign_in_params... etc) on all devise controllers.

And after i solved this issue, i found out that warden authentication also failed due to this change in the request params.

Specifically the valid_for_params_auth? method on the Devise::Strategies::Authenticatable strategy

pcriv avatar Feb 18 '18 23:02 pcriv

Seems like the devise_parameter_sanitizer solution should work. I'm willing to accept a pull request that makes it possible to do it like in the provided example.

tegon avatar Mar 30 '18 14:03 tegon

Hello,

I am looking to make my first open source contribution. Is this a good issue to take on? If not, feel free to point me other issues that might be good for someone that's new to open source. Thanks!

aamarill avatar May 14 '18 18:05 aamarill

@aamarill I'm not sure what is the complexity of this one. It might be difficult or not. If you want to work on this, I can help with any doubts you have along the way. I'd start by creating a failing test of the desired behavior. The code mentioned on the issue should work, but currently, it doesn't:

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_in) do |user_params|
    x = params.require(:data).permit(:type, { attributes: [:email, :password] })
    x[:attributes]
  end
end

So I think it's a good start to create a test with this implementation and them try to find out what's missing.

tegon avatar May 15 '18 22:05 tegon

@tegon I think this method is also related:

https://github.com/plataformatec/devise/blob/1c8e97c75ace52be4e2d0531dd4fc70e95d3cb83/lib/devise/strategies/authenticatable.rb#L77

pcriv avatar May 16 '18 07:05 pcriv