【问题标题】:Override the Devise redirect path, using params?使用参数覆盖设计重定向路径?
【发布时间】:2018-05-10 12:09:13
【问题描述】:

我已遵循指南How To: Override confirmations so users can pick their own passwords as part of confirmation activation。我一切顺利。

我的邮件最终会发送一个确认链接:

"https://example.com/users/confirmation?confirmation_token=foo"

在用户选择了他的新密码后,确保用户被重定向到标准的after_sign_in_path_for

def do_confirm
  @confirmable.confirm
  set_flash_message :notice, :confirmed
  sign_in_and_redirect(resource_name, @confirmable)
end

但是,如果我想将用户重定向到专门为该用户量身定制的单独页面怎么办?比如:

"https://example.com/users/confirmation?confirmation_token=foo&redirect_to=#{CGI.parse('custom_path')}"

我该如何设置?

【问题讨论】:

  • 通过使用您喜欢的任何逻辑覆盖after_sign_in_path_for 方法,可能在ApplicationController 中。
  • 请注意,有一个潜在的安全问题需要注意:如果您盲目地重定向到 any redirect_to 值,那么恶意用户可能会发送如下链接: "https://example.com/users/sign_in&redirect_to=http://www.my-fake-copy-of-your-website.com/scam"。因此,您需要按照以下方式进行检查:URI.parse(params[:redirect_to]).hostname == hostname

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


【解决方案1】:

正如 Tom 在上面的评论中所说,您想要覆盖 after_sign_in_path_for 方法。

devise's GitHub wiki 上有一个页面更详细地解释了这一点并提供了一个代码示例(如果您使用 OAuth,它看起来会有些不同):

class ApplicationController < ActionController::Base
  protect_from_forgery

  protected  
    def after_sign_in_path_for(resource)
      sign_in_url = new_user_session_url
      if request.referer == sign_in_url
        super
      else
        stored_location_for(resource) || request.referer || root_path
      end
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多