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



我正在开发一个电子商务应用程序。当用户登录到我的应用程序时,我想检查我的外部订阅处理程序,并确保他们的订阅仍然是活跃的,而不是过期/失败/什么的。

我成功地弄清楚了如何在我的initializers/devise.rb中使用Warden回调来执行登录后对模型的检查。然而,如果有问题,我想再次注销他们,并重定向到一个特定的页面,告诉他们下一步该做什么。

这是我有的。我知道我不能从回调中使用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

让回调引发异常并在控制器中从中拯救。例如:

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

你也可以像这样在你的ApplicationController中使用rescue_from:

class ApplicationController
  rescue_from SubscriptionExpiredException, :with => :deny_access
  def deny_access
    redirect_to "/account/expired"
  end
end

相关内容

最新更新