gpt4 book ai didi

ruby-on-rails - 如何覆盖设计确认指令

转载 作者:行者123 更新时间:2023-12-02 04:30:10 25 4
gpt4 key购买 nike

我正在使用 Devise 并尝试以编程方式创建新用户(客户)并通过电子邮件向客户发送密码。我创建了一个自定义邮件程序并覆盖了confirmation_instructions 方法。下面的代码工作正常。在我以编程方式创建客户后,系统会向客户发送一封带有自定义 header “Bar”的电子邮件,并发送确认链接。

class CustomMailer < Devise::Mailer   
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`

def confirmation_instructions(record, token, opts={})
headers["Custom-header"] = "Bar"
super
end
end

但我还需要发送密码,所以我尝试了以下方法:

class CustomMailer < Devise::Mailer   
helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`

def confirmation_instructions(record, token, opts={})
headers["Custom-header"] = "Bar"
@password = opts[:password]
super
end
end

我从我的 Controller 手动调用电子邮件方法,如下所示:

@customer_instance = Customer.new({ :email => customer_params[:email], :password => generated_password, :password_confirmation => generated_password, :first_name => customer_params[:first_name], :last_name => customer_params[:last_name] })
@customer_instance.skip_confirmation!
@customer_instance.save

CustomMailer.confirmation_instructions(@customer_instance, @customer_instance.confirmation_token, {password: generated_password}).deliver

密码发送正常,但确认 token 未发送。如何使用 Devise 创建确认 token 以传递到此处?或者是否有其他方法可以让我不需要手动传递 token ?

最佳答案

分配临时密码很危险,因为用户很可能懒得更改它。此外,电子邮件可能会被其他人拦截,从而迫使合法用户更改密码。现在该帐户位于其他人的 Controller 中。

相反,在创建帐户时不要指定任何密码。使用设计的:confirmable模块:

devise :database_authenticatable, :confirmable, etc...

何时 confirmable使用后,一旦保存记录,devise 就会自动向用户发送一封电子邮件,其中包含单击以确认帐户的链接。当单击该链接时,devise 将更新数据库以将记录标记为确认,然后将它们重定向到 Rails 项目的 root_path。

此行为在方法 after_confirmation_path_for 中定义,因此请在您的 ApplicationController 中覆盖该方法,并让它返回强制设置密码的页面的 url(例如 edit_user_path(current_user) 如果您的设备型号为 User )。

此外,将 before_filter 添加到您的 ApplicationController 中,以便他们在继续之前强制重置密码。

class ApplicationController < ActionController::Base
private
before_filter :force_user_to_set_password
def force_user_to_set_password
redirect_to edit_user_path(current_user), :notice => "First, create a password" if user_signed_in? && current_user.encrypted_password.blank?
end

def after_confirmation_path_for(resource_name, resource)
__send__("edit_#{resource_name}_path", resource)
end
end

当然,还要防止它们陷入重定向循环:

class UsersController < ApplicationController
private
skip_before_filter :force_user_to_set_password, :only => [:edit, :update]
end

关于ruby-on-rails - 如何覆盖设计确认指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23715507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com