the*_*ler 2 ruby-on-rails devise
我正在使用 Devise 并尝试以编程方式创建一个新用户(客户)并通过电子邮件向客户发送密码。我创建了一个自定义邮件程序并覆盖了confirmation_instructions 方法。下面的代码工作正常。在我以编程方式创建客户后,系统会向客户发送一封带有自定义标头“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
Run Code Online (Sandbox Code Playgroud)
但我还需要发送密码,所以我尝试了以下方法:
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
Run Code Online (Sandbox Code Playgroud)
我从控制器手动调用电子邮件方法,如下所示:
@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
Run Code Online (Sandbox Code Playgroud)
密码发送正常,但确认令牌未发送。如何使用 Devise 创建确认令牌以传递到此处?或者是否有其他方法可以让我不需要手动传递令牌?
分配临时密码很危险,因为用户很可能懒得更改它。此外,电子邮件可能会被其他人拦截,从而迫使合法用户更改密码。现在该帐户位于其他人的控制器中。
相反,在创建帐户时不要指定任何密码。使用 Devise 的:confirmable模块:
devise :database_authenticatable, :confirmable, etc...
Run Code Online (Sandbox Code Playgroud)
使用时confirmable,一旦保存记录,设备会自动向用户发送一封电子邮件,其中包含点击确认帐户的链接。当单击该链接时,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
Run Code Online (Sandbox Code Playgroud)
当然,还要防止它们陷入重定向循环:
class UsersController < ApplicationController
private
skip_before_filter :force_user_to_set_password, :only => [:edit, :update]
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6045 次 |
| 最近记录: |