1

I just want to modify the value of the password parameter in

Devise::SessionsController my current code looks like this:

class Api::V1::SessionsController < Devise::SessionsController

  before_action :override_params, only: [:create]

  private

  def override_params
    params[:user][:password] = '123123123'   
  end

end

In the code above, I'm trying to modify the value of user's password but when I try to login, seems like it doesn't override the user password params. I'm still getting the value that I entered on the URL not the one I'm overridden in controller.

AllenC
  • 2,754
  • 1
  • 41
  • 74
  • Possible duplicate of [Automatically set password for user - Devise](https://stackoverflow.com/questions/28398803/automatically-set-password-for-user-devise) – Mayur Shah Jul 03 '17 at 10:21
  • @MayurShah this one is different. It's assigning a value to an ActiveRecord, not parameters – AllenC Jul 03 '17 at 10:25
  • have you tried something like this: user.password = user.password_confirmation = password in override_params – Divya Sharma Jul 03 '17 at 10:32

1 Answers1

0

Something like this should work (untested)

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_in) do |user_params|
      u_params = user_params.permit(:username, :email)
      u_params[:password] = '123123123'
      u_params.permit!
    end
  end
end
Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36
  • Thanks for this solution, but still cannot modify the value of the password parameter. I think devise is user resource_params but I cannot modify the value of this. :( – AllenC Jul 03 '17 at 10:55
  • can you provide more context as to what and why you need this? maybe there's something else that can be done – Md. Farhan Memon Jul 03 '17 at 10:58
  • I'm creating an API to decrypt the password came from an API request of iOS app. So basically the iOS app will pass to me the encrypted password then on my end I need to decrypt it. I want to modify the user[:password] params to decrypted password, but I cannot override the value of it. The decryption part is already working I tested it on my rails console. – AllenC Jul 03 '17 at 11:01
  • so you are using devise_token_auth, or just devise? You will have to override whole create action in sessions_controller in this case – Md. Farhan Memon Jul 03 '17 at 11:04
  • I'm just using devise. – AllenC Jul 03 '17 at 11:10