设计 - 用户登录后运行检查,如果错误则重定向

tkr*_*car 2 devise warden ruby-on-rails-3

我正在开发一个电子商务应用程序.当用户登录我的应用程序时,我想检查我的外部订阅处理程序并确保他们的订阅仍处于活动状态且未过期/失败/无论如何.

我成功地想出了如何使用Warden回调initializers/devise.rb来登录后对模型进行检查.但是,如果出现问题,我想再次将其注销并重定向到某个页面,告诉他们下一步该做什么.

这就是我所拥有的.我知道我不能redirect_to从回调中使用.鉴于此,做我正在尝试做的最好的方法是什么?

Warden::Manager.after_authentication do |user, auth, opts|
  begin
    user.check_active_subscription # this works, and will raise one of several exceptions if something is goofy
  rescue
    redirect_to "/account/expired" # obviously this won't work, but see what I'm trying to do?
  end
end
Run Code Online (Sandbox Code Playgroud)

Mis*_*cha 8

只需让回调引发异常并在控制器中进行抢救.例如:

Warden::Manager.after_authentication do |user, auth, opts|
  user.check_active_subscription
end

class SessionsController < ApplicationController
  def create
    # Authenticate
  rescue SubscriptionExpiredException
    # Logout
    redirect_to "/account/expired"
  end
end
Run Code Online (Sandbox Code Playgroud)

你也可以使用rescue_from你的ApplicationController是这样的:

class ApplicationController
  rescue_from SubscriptionExpiredException, :with => :deny_access

  def deny_access
    redirect_to "/account/expired"
  end
end
Run Code Online (Sandbox Code Playgroud)