简体   繁体   English

忘记密码设计 gem API

[英]Forgot password Devise gem API

I have been using Devise gem in my application.我一直在我的应用程序中使用 Devise gem。 I could able to configure devise sessions_controller to respond to both request from web and from mobile API call.我可以配置设计 session_controller 来响应来自 Web 和移动 API 调用的请求。

But now i am trying to see how i can use the Forgot Password option of Devise gem for Mobile API call.但现在我想看看如何使用 Devise gem 的忘记密码选项进行移动 API 调用。 I can able to use the sign in with API as like below我可以使用 API 登录,如下所示

curl -X POST 'http://localhost:3002/users/sign_in.json' -d 'user[email]=balan@test.com&user[password]=123456'

can i do the same with forgot password?我可以用忘记密码做同样的事情吗?

Got the answer.得到了答案。

1) Create a custom action which recieves email as input 1)创建一个接收email作为输入的自定义操作

2) Add below code 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

I did this:我这样做了:

In config/routes.rb :config/routes.rb

namespace :api do
  namespace :v1 do 
     resources :reset_passwords, only: [:index, :create]
  end
end

and in app/controllers/api/v1/reset_passwords_controller.rb :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

devise now supports it.设计现在支持它。 Check the link bellow https://github.com/plataformatec/devise/wiki/API-Mode-Compatibility-Guide检查下面的链接https://github.com/plataformatec/devise/wiki/API-Mode-Compatibility-Guide

Password Reset Request (post)密码重置请求(发布)

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"
  }
}'

Password Reset Confirm (patch)密码重置确认(补丁)

  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.

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