【问题标题】:Rails 6, Devis: How to confirm user after successful email confirmationRails 6,Devis:电子邮件确认成功后如何确认用户
【发布时间】:2021-03-19 21:09:19
【问题描述】:

我正在使用Rails 6React Devise & GraphQl & devise-token_authenticatable 进行身份验证的应用程序。

我正在尝试在用户成功确认其电子邮件后对其进行确认。在搜索并找到一些可能的答案后,我尝试了这个: 在设计中>> Confirmation_controller.rb

class Devise::ConfirmationsController < DeviseController

  protected

    # The path used after confirmation.
    def after_confirmation_path_for(resource_name, resource)
      if signed_in?(resource_name)
        redirect_to app_root_url
      else

        resource.confirm!
        redirect_to "#{request.base_url}/app/login", notice: "Email confirmed!"
      end
    end

end

但这并没有按预期工作。

我还尝试了以下方法,但没有任何运气:

def after_confirmation
  self.confirm!
end

有什么建议吗? 感谢您的宝贵时间

【问题讨论】:

  • 您实际上不需要做任何事情。只需确保您已添加 :confirmable 以在您的模型中进行设计,运行迁移即可。说明非常简单 - github.com/heartcombo/devise/wiki/…
  • 谢谢!我确实在用户模型中启用了:confirmable。由于我的设置 (devise_for :users, skip: :sessions, controllers: { confirmations: 'users/confirmations' } ),我的 routes.rb 文件中有 skip: :sessions 除外。这可能与它有关。我必须找到一种不同的方法来完成这项工作。

标签: ruby-on-rails devise devise-confirmable


【解决方案1】:

好吧,我终于想通了。这是我的解决方案。我刚刚在Users::ConfirmationsController#show 操作中更新了用户confirmed_at 属性。

class Users::ConfirmationsController < Devise::ConfirmationsController
  # GET /resource/confirmation/new
  # def new
  #   super
  # end

  # POST /resource/confirmation
  # def create
  #   super
  # end

  # GET /resource/confirmation?confirmation_token=abcdef
  def show
    @user = User.find_by_confirmation_token(params[:confirmation_token])
    @user.update(confirmed_at: Time.now.utc)
    # super
  end
end 

在 routes.rb 中 devise_for :users, skip: :sessions, controllers: { confirmations: 'users/confirmations' }

【讨论】:

  • 可以用@user.confirm确认专用方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2011-01-06
  • 2018-04-02
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多