我正在使用Devise + ominauth2,以允许用户通过不同的服务登录。如果他们登录,他们会获得更好的体验,但是如果不是这样,生活将会继续。但是,我看不到Devise有一种方法来创建一个既允许经过身份验证的访问,又允许未经身份验证的访问的页面。
例如:
class SomeController < ApplicationController
before_action :authenticate_user!
def some_action
end
end
在上述控制器中,只有用户进行身份验证,some_action才可访问。但是我真正想要的是更多类似的东西:
class SomeController < ApplicationController
def some_action
loadSessionFromCookieIfExists
end
end
然后,我可以使用user_signed_in吗?在我看来。但这不起作用,因为authenticate_user!会将它们重定向到其他地方。除非有一个authenticate_if_possible方法?
所以我现在所拥有的,实际上是:
class SomeController < ApplicationController
before_action :authenticate_user!
skip_before_action :authenticate_user!, :only=>[:no_auth]
def action
do_stuff
return
end
def no_auth
do_stuff
return
end
private
def do_stuff
render :template => "action"
end
end
我应该补充一点,omniauth2不支持令人难以忘怀,因此很有可能这一切都可以正常工作,而问题出在omniauth2上。