【问题标题】:How to generate link to restore password using devise?如何使用设计生成恢复密码的链接?
【发布时间】:2020-03-19 18:10:06
【问题描述】:

我正在使用 Mailer 和 MailCatcher。

当管理员在系统中注册客户时,客户应该会收到一封电子邮件,其中包含恢复密码的链接

【问题讨论】:

  • 您的意思是在管理员创建密码后设置密码吗?你看过github.com/scambra/devise_invitable吗?
  • 请不要随意使用粗体字。它就像一种香料,随意使用或过多使用会导致消化不良。文本中单独部分的有用标题,类似于参考书,是一回事,但调用文件名并没有帮助。

标签: ruby-on-rails ruby devise actionmailer mailcatcher


【解决方案1】:

您正在重新发明轮子。 Devise 已经有一个 Invitable module 可以满足您的需求。

按照安装说明操作后,您只需添加一个指向new_user_invitation_path 的链接,管理员可以在其中邀请其他用户。如果您想锁定邀请以便只有管理员可以邀请用户,只需自定义控制器:

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

如果你真的想重新发明轮子,那就去做吧。 Devise 不会在 Devise::RegistrationsController 中重置密码 - 你也不应该这样做。

密码重置在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

密码重置电子邮件中的链接是/users/password/edit。您可以在email view 中看到。

edit_password_url(@resource, reset_password_token: @token)

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 2013-02-10
    • 1970-01-01
    • 2010-11-13
    • 2012-01-22
    • 2014-01-17
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多