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?
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"
}
}'
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With