【问题标题】:Gitlab email confirmation redirectGitlab 电子邮件确认重定向
【发布时间】:2019-07-02 01:46:15
【问题描述】:

默认情况下,Gitlab 在电子邮件确认后将用户重定向到主页。我想改为异地重定向。

我不认为有一个配置选项,所以我问如何破解它。

我找到了confirmations_controller.rb

# frozen_string_literal: true

class ConfirmationsController < Devise::ConfirmationsController
  include AcceptsPendingInvitations

  def almost_there
    flash[:notice] = nil
    render layout: "devise_empty"
  end

  protected

  def after_resending_confirmation_instructions_path_for(resource)
    users_almost_there_path
  end

  def after_confirmation_path_for(resource_name, resource)
    accept_pending_invitations

    # incoming resource can either be a :user or an :email
    if signed_in?(:user)
      after_sign_in(resource)
    else
      Gitlab::AppLogger.info("Email Confirmed: username=#{resource.username} email=#{resource.email} ip=#{request.remote_ip}")
      flash[:notice] = flash[:notice] + " Please sign in."
      new_session_path(:user, anchor: 'login-pane')
    end
  end

  def after_sign_in(resource)
    after_sign_in_path_for(resource)
  end
end

如何让它将我重定向到 google.com?

【问题讨论】:

标签: ruby redirect devise gitlab


【解决方案1】:

这是一个自定义确认控制器吗?如果没有在 app/controllers 目录下新建一个confirmations_controller.rb:

class ConfirmationsController < Devise::ConfirmationsController
  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 your_desired_redirect_path }
    else
      respond_with_navigational(resource.errors, status: :unprocessable_entity){ render_with_scope :new }
    end
  end
end

config/routes.rb 中,添加此行,以便Devise 使用您的自定义ConfirmationsController。这假设 Devise 对用户表进行操作(您可以编辑以匹配您的表)。

devise_for :users, controllers: { confirmations: 'confirmations' }

答案链接:How to make Devise redirect after confirmation

【讨论】:

  • 感谢config/routes.rb 看起来有两条新线:# Sign up get 'users/sign_up/welcome' =&gt; 'registrations#welcome' patch 'users/sign_up/update_registration' =&gt; 'registrations#update_registration' 我不知道 ConfirmationsController 是否是自定义的。我刚刚编辑了我在app/controllers/confirmations_controller.rb 中找到的那个。也许这两条新线会覆盖它?
  • 如果你想覆盖你可以这样做。
【解决方案2】:

after_confirmation_path_for 方法返回。以下是 Google 的示例。

class ConfirmationsController < Devise::ConfirmationsController
  ...

  protected


  def after_confirmation_path_for(resource_name, resource)
    ...
    'https://www.google.com' # assuming you're redirecting to Google
  end

end

【讨论】:

  • 感谢您,这在您回答时有效,但不幸的是,在更新到 12.6 后它不再有效。知道如何解决吗?
  • 更新代码后,删除选定的答案是不公平的。如果事情发生了变化,问题就会改变,这不是回答的人的错。相反,需要使用所有新信息创建一个新问题。请参阅我上面评论中的链接页面。
  • 赞成确保 Amin 至少获得一半的赏金,尽管这个答案应该被接受。 (并因此获得了全部赏金)
  • @theTinMan 我认为 SO 应该是真实文档的存储库。如果我接受这个来自谷歌的人,尽管当时它正在工作,但它不会得到正确的答案。
  • @Harry 不,不是。它应该是关于提出一个适用于您的问题并根据该问题获得答案。这绝对不是问一个问题,得到一个准确和正确的答案,然后以一种使答案无效的方式改变你的代码库。此答案的唯一问题是您在提出问题并收到答案后移动了球门柱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 2017-03-21
相关资源
最近更新 更多