[英]Change Devise Password Manually
Ok what i have is an API that i built for our customer service team. 好吧,我拥有的是我为客户服务团队构建的API。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.