[英]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.