0

我使用设计通过设计可确认选项对我的网站上的用户进行身份验证。现在,当用户注册时,他们确实会收到一封确认电子邮件,但我需要用户在发送确认后自动登录。

我希望用户即使不确认他们的电子邮件地址也能登录。

我知道我需要修改确认控制器,但不知道是什么以及如何。这是我的代码:

class Devise::ConfirmationsController < DeviseController
# GET /resource/confirmation/new
def new
 build_resource({})
end

# POST /resource/confirmation
def create
 self.resource = resource_class.send_confirmation_instructions(resource_params)
if successfully_sent?(resource)
  respond_with({}, :location => 
after_resending_confirmation_instructions_path_for(resource_name))
else
  respond_with(resource)
 end
end

 # GET /resource/confirmation?confirmation_token=abcdef
def show
 self.resource = resource_class.confirm_by_token(params[:confirmation_token])

if resource.errors.empty?
  set_flash_message(:notice, :confirmed) if is_navigational_format?
  sign_in(resource_name, resource)
   respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
 else
  respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
 end
end

protected

# The path used after resending confirmation instructions.
def after_resending_confirmation_instructions_path_for(resource_name)
  new_session_path(resource_name)
end

# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
  after_sign_in_path_for(resource)
end

 end
4

1 回答 1

2

如果您查看设计初始化程序,在第 87 行附近,您应该会看到:

# ==> Configuration for :confirmable
# A period that the user is allowed to access the website even without
# confirming his account. For instance, if set to 2.days, the user will be
# able to access the website for two days without confirming his account,
# access will be blocked just in the third day. Default is 0.days, meaning
# the user cannot access the website without confirming his account.
# config.allow_unconfirmed_access_for = 2.days

启用此功能后,您的用户无需通过确认电子邮件确认其帐户即可登录一段时间。如果您想在发送确认电子邮件后自动让用户登录,那么您将需要覆盖createDevise confirms_controller 中的方法。那里有关于如何覆盖 Devise 控制器的教程,但您需要执行以下操作:

def create
  self.resource = resource_class.send_confirmation_instructions(resource_params)

  if successfully_sent?(resource)
    sign_in(resource_name, resource)
    respond_with({}, :location => after_sign_up_path_for(resource)
  else
    respond_with(resource)
  end
end

这基本上是默认的 Devise 方法,带有额外的行来登录用户并将respond_Withwith 调用更改为 be after_sign_up_path_for。请注意,after_sign_up_path_for默认为根路由,这可能是不可取的。Devise Wiki 上有关于覆盖此路径的示例。

于 2012-08-19T15:36:40.473 回答