[英]Forgot password Devise gem API
我一直在我的应用程序中使用 Devise gem。 我可以配置设计 session_controller 来响应来自 Web 和移动 API 调用的请求。
但现在我想看看如何使用 Devise gem 的忘记密码选项进行移动 API 调用。 我可以使用 API 登录,如下所示
curl -X POST 'http://localhost:3002/users/sign_in.json' -d 'user[email]=balan@test.com&user[password]=123456'
我可以用忘记密码做同样的事情吗?
得到了答案。
1)创建一个接收email
作为输入的自定义操作
2)添加以下代码
@user = User.find_by_email("email@email.com")
if @user.present?
@user.send_reset_password_instructions
render :text => "updated"
else
render :text => "no such email"
end
我这样做了:
在config/routes.rb
:
namespace :api do
namespace :v1 do
resources :reset_passwords, only: [:index, :create]
end
end
在app/controllers/api/v1/reset_passwords_controller.rb
:
class Api::V1::ResetPasswordsController < Api::V1::BaseController
def index
user = User.find_by_email(user_params)
if user.present?
user.send_reset_password_instructions
render(
json: "{ \"result\": \"Email already exists\"}",
status: 201
)
else
render(
json: "{ \"error\": \"Not found\"}",
status: 404
)
end
end
private
def user_params
params.require(:email)
end
end
设计现在支持它。 检查下面的链接https://github.com/plataformatec/devise/wiki/API-Mode-Compatibility-Guide
密码重置请求(发布)
curl -X POST \
'http://{{protocol}}{{host}}:{{port}}/{{resource_plural}}/password' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 406d7e49-7da5-460d-8cc2-1009da8fcb73' \
-H 'cache-control: no-cache' \
-d '{
"user": {
"email": "user@example.com"
}
}'
密码重置确认(补丁)
curl -X PATCH \
'http://{{protocol}}{{host}}:{{port}}/{{resource_plural}}/password' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: f68be2db-1b90-4443-be93-3f012ca4c7dc' \
-H 'cache-control: no-cache' \
-d '{
"user": {
"reset_password_token": "-96mB4zMSjL4t5N6ozGC",
"password": "newpassword",
"password_confirmation": "newpassword"
}
}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.