How to generate link to restore password using devise?

307 Views Asked by At

I am using Mailer and MailCatcher.

When an admin registered the customer in the system, the customer should receive an email with a link to restore his password

1

There are 1 best solutions below

0
max On

You're reinventing the wheel. Devise already has an Invitable module that does what you are looking for.

After following the installation instructions you just add a link to new_user_invitation_path where admins can invite other users. If you want to lock down the invitiations so that only admins can invite users just customize the controller:

class MyInvitationsController < Devise::InvitationsController
  before_action :authorize_admin!, only: [:new, :create]

  def authorize_admin!
    # if you are using Pundit
    authorize resource_class, :invite?

    # or if you're reinventing the authorization wheel
    redirect_to '/somewhere'
  end
end

If you really want to reinvent the wheel do it right. Devise does not do password resets in Devise::RegistrationsController - neither should you.

Password resets are done in Devise::PasswordsController:

                               Prefix Verb   URI Pattern                                                                              Controller#Action                                                               
                    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
                   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
                        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                                      PUT    /users/password(.:format)                                                                devise/passwords#update
                                      POST   /users/password(.:format)                                                                devise/passwords#create

The link in the password reset email is /users/password/edit. Which you can see in the email view.

edit_password_url(@resource, reset_password_token: @token)