Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forgot password Devise gem API

I have been using Devise gem in my application. I could able to configure devise sessions_controller to respond to both request from web and from mobile API call.

But now i am trying to see how i can use the Forgot Password option of Devise gem for Mobile API call. I can able to use the sign in with API as like below

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

can i do the same with forgot password?

like image 404
balanv Avatar asked Feb 12 '13 06:02

balanv


3 Answers

devise now supports it. Check the link bellow 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": "[email protected]"
  }
}'

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"
  }
}'
like image 57
oaamados Avatar answered Nov 12 '22 05:11

oaamados


Got the answer.

1) Create a custom action which recieves email as input

2) Add below code

@user = User.find_by_email("[email protected]")
if @user.present?
 @user.send_reset_password_instructions
 render :text => "updated"
else
     render :text => "no such email"
end
like image 42
balanv Avatar answered Nov 12 '22 05:11

balanv


I did this:

In 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:

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
like image 2
carlitos Avatar answered Nov 12 '22 06:11

carlitos