简体   繁体   中英

Change Devise Password Manually

Ok what i have is an API that i built for our customer service team. We can create accounts and users and everything works great.

But when i built it to Change the user passwords it changes them but will not login.

to create an account i have this coding:

  def create
    params["user"].delete("user_id") // this is being sent in from the api..
    params["user"]["encrypted_password"] = Password.new(Password.create(params["user"]["password"]))
    params["user"]["password"] = Password.new(Password.create(params["user"]["password"]))
    user = User.create(params["user"])
    if user
      render json: {company: user}, status: 200
    else
      render json: {message: "Problem creating company"}, status: 500
    end
  end

that works perfectly

but when i go to update a user the same:

  params["user"]["encrypted_password"] = Password.new(Password.create(password))
  params["user"]["password"] = Password.new(Password.create(password))

Does not work correctly

def update
    user = User.find(params[:id])
    password = params["user"].delete("user_password")
    if password && !password.blank?
      puts password
      params["user"]["encrypted_password"] = Password.new(Password.create(password))
      params["user"]["password"] = Password.new(Password.create(password))
    end

    if user.update_attributes(params["user"])
      render json: {company: user}, status: 200
    else
      render json: {message: "Problem updating company"}, status: 500
    end
  end

It will not log-in after changeing

You don't need to set the password in that way, just set the password and password confirmation and devise will take care of the rest, don't worry, it won't be stored in plain text, devise automatically converts the plain text into hashed password before saving it to the db

def update
  user = User.find(params[:id])
  password = params["user"].delete("user_password")
  if password && !password.blank?
      user.password = user.password_confirmation = password
  end

  if user.save
    render json: {company: user}, status: 200
  else
    render json: {message: "Problem updating company"}, status: 500
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM