用设计确认后自动登录

Phi*_*899 10 ruby ruby-on-rails devise devise-confirmable ruby-on-rails-4

我正在使用设计确认.我有一些自定义的东西,我需要覆盖设计的确认!方法,所以在我的用户模型中,我有以下方法覆盖它:

def confirm!
  super
  gb = Gibbon::API.new(ENV['MAILCHIMP_API_KEY'])
  gb.lists.subscribe({:id => ENV['MAILCHIMP_ID'], :email => {:email => self.email }})
end
Run Code Online (Sandbox Code Playgroud)

这非常有效.现在我想要它,以便用户在确认后自动登录,但无法弄清楚如何.我知道这被认为是一个安全漏洞,但我已经承受了风险,因此我的网站用户体验值得.我不想对routes文件做任何事情,因为这个方法已经有效,所以我应该可以从这里开始.我已经尝试将以下内容放入我的配置文件中:

config.allow_insecure_sign_in_after_confirmation = true
Run Code Online (Sandbox Code Playgroud)

但它没有签署用户.

我看过堆栈溢出页面在使用devise gem确认链接点击后避免登录?它没有用,所以请不要将其标记为副本.

感谢大家.

小智 20

你会改变routes.rb和controllers/users/confirmations_controller.rb(也许是默认路径)

routes.rb映射用户/ confrimations_controller

devise_for :users, controllers: {confirmations: 'users/confirmations'}
Run Code Online (Sandbox Code Playgroud)

confirmmations_controller自动登录并重定向到

def after_confirmation_path_for(resource_name, resource)
  sign_in(resource)
  any_path # redirect_to is not necessary
end
Run Code Online (Sandbox Code Playgroud)


mar*_*vor 10

生成控制器:

rails generate devise:controllers users -c=confirmations
Run Code Online (Sandbox Code Playgroud)

修改路线:

devise_for :users, controllers: {confirmations: 'users/confirmations'}
Run Code Online (Sandbox Code Playgroud)

然后覆盖该show方法:

  # app/controllers/users/confirmations_controller.rb
  def show
    super do
      sign_in(resource) if resource.errors.empty?
    end
  end

  def after_confirmation_path_for(resource_name, resource)
    after_sign_in_path_for(resource)
  end
Run Code Online (Sandbox Code Playgroud)