我一直在应用程序中使用Devise gem。我可以配置designsession_controller以响应来自web和来自移动API调用。
但现在我正试着看看如何使用Devise宝石的忘记密码选项用于移动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
design现在支持它。查看下面的链接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"
}
}'
我做了这个:
在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