如何使用 Ruby on Rails 和 Devise Gem 重定向未经确认的用户

发布于 2024-12-21 19:28:48 字数 529 浏览 1 评论 0原文

我想更改设备的用户登录行为,以便在未确认用户时将用户重定向到不同的页面。

我的 app/controllers/sessions_controller.rb 中有以下代码:

  # POST /user/sign_in
  def create

    @user = warden.authenticate!(:scope => :user) 
    sign_in @user
     respond_with @user, :location => after_sign_in_path_for(@user)

  end

当用户输入正确的用户名/密码但未经确认时,Warden 将引发错误,将用户重定向到登录页面(代码 302)。同时这会将 flash[:alert] 置于“未确认”状态。

有没有办法将未经确认的用户重定向到特定页面?

我解决这个问题的想法是读取 flash[:alert] 值,如果该值“未经确认”但典狱长没有发送有关用户的附加数据,则重定向到适当的页面。

I would like to change the user sign-in behaviour of devise in a way to redirect a user to a different page when that user is not confirmed.

I have the following code in my app/controllers/sessions_controller.rb:

  # POST /user/sign_in
  def create

    @user = warden.authenticate!(:scope => :user) 
    sign_in @user
     respond_with @user, :location => after_sign_in_path_for(@user)

  end

When the user has put correct username/password and is not confirmed Warden will raise an error that will redirect the user to the sign in page (code 302). At the same time this will put flash[:alert] to "unconfirmed".

Is there a way to redirect an unconfirmed user to a specific page?

My idea to work around this was reading the flash[:alert] value and redirecting to the appropriate page if the value is "unconfirmed" but warden is not sending additional data about the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为你拒绝所有暧昧 2024-12-28 19:28:48
class SessionsController < Devise::SessionsController
  def create
    @user = User.where(:email => params[:user][:email])[0] # you get the user now
    if @user.confirmed?
      super
    else
      redirect_to YOUR_DESIRED_path # or something else you want to do
    end
  end
end
class SessionsController < Devise::SessionsController
  def create
    @user = User.where(:email => params[:user][:email])[0] # you get the user now
    if @user.confirmed?
      super
    else
      redirect_to YOUR_DESIRED_path # or something else you want to do
    end
  end
end
心如荒岛 2024-12-28 19:28:48

您需要一个自定义的 FailureApp 来执行此操作(并且您需要在路线中定义 /unconfirmed 等,就像任何其他 Rails 操作一样):

lib/custom_failure.rb

class CustomFailure < Devise::FailureApp
  def redirect_url
    if warden_message == :unconfirmed
      '/unconfirmed'
    else
      super
    end
  end

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

config/initializers/devise.rb:

  config.warden do |manager|
    manager.failure_app = CustomFailure
  end

You need a custom FailureApp to do this (and you need to define /unconfirmed in your routes, etc., like any other Rails action):

lib/custom_failure.rb

class CustomFailure < Devise::FailureApp
  def redirect_url
    if warden_message == :unconfirmed
      '/unconfirmed'
    else
      super
    end
  end

  # You need to override respond to eliminate recall
  def respond
    if http_auth?
      http_auth
    else
      redirect
    end
  end
end

config/initializers/devise.rb:

  config.warden do |manager|
    manager.failure_app = CustomFailure
  end
老娘不死你永远是小三 2024-12-28 19:28:48

我认为要使其正常工作,您需要将 Brian 的答案与您的routes.rb 文件中的添加内容结合起来。

我现在要对其进行测试,如果/当我让它工作时我会给你更新。 (编辑:我已经在本地工作了)。

您需要执行以下操作:

  1. your_rails_app/app/controllers 中创建一个名为 sessions_controller.rb 的新文件,该文件应如下所示:

    类 SessionsController <设计::SessionsController
      定义创建
        @user = User.where(:email => params[:user][:email])[0] # 现在你就得到了用户
        如果@user.confirmed?
          极好的
        别的
          redirect_to YOU​​R_DESIRED_path # 或其他你想做的事情
        结尾
      结尾
    结尾
    
  2. 将以下行添加到你的routes.rb文件(或者修改你的routes.rb文件中包含devise_for :users的行......或者你的模型的名称):

    devise_for:用户,控制器:{会话:“会话”}
    
  3. 您需要将其添加到您的 User 模型中:

    <前><代码>受保护
    def 需要确认吗?
    错误的
    结尾

I think that to get this working you need to combine Brian's answer with an addition to your routes.rb file.

I'm going to test it out right now so I'll give you an update if/when I get it working. (EDIT: I've got this working locally).

Here's what you need to do:

  1. Create an new file in your_rails_app/app/controllers called sessions_controller.rb which should look like this:

    class SessionsController < Devise::SessionsController
      def create
        @user = User.where(:email => params[:user][:email])[0] # you get the user now
        if @user.confirmed?
          super
        else
          redirect_to YOUR_DESIRED_path # or something else you want to do
        end
      end
    end
    
  2. Add the following line to your routes.rb file (or modify the line in your routes.rb file that contains devise_for :users ...or whatever your model is called):

    devise_for :users, controllers: { sessions: "sessions" }
    
  3. You need to add this to your User model:

     protected
     def confirmation_required?
       false
     end
    
在梵高的星空下 2024-12-28 19:28:48

如果您不想允许未经确认的用户登录,请将此行放入 devise.rb 中
注册成功后。

config.allow_unconfirmed_access_for = nil 

Please put this line into devise.rb, if you do not want to allow unconfirmed user to sign in
after successful registration.

config.allow_unconfirmed_access_for = nil 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文