用户可以通过OmniAuth或通过电子邮件和密码(但只有在被邀请的情况下)才能登录我们的应用程序。
我们有一种方法,可以检查用户是否可以通过电子邮件和密码登录,如下所示:
def active_for_authentication?
super && valid_for_authentication
end
private
# Check wheter user was invited or not
def valid_for_authentication
User.invitation_accepted.find_by(id: id).present?
end
这工作正常,但是当我想通过OmniAuth登录时,此方法将阻止我。 如果使用OmniAuth身份验证,是否可以指定绕过此方法?
答案 0 :(得分:1)
您可以在执行active_for_authentication之前检查以下方法?线
如果使用omniauth登录,以下行将返回如果返回的 res 数据,则添加逻辑除非存在res.present
def logged_using_omniauth? request
res = nil
omniauth = request.env["omniauth.auth"]
res = Authentication.find_by_provider_and_uid
(omniauth['provider'], omniauth['uid']) if omniauth
res
end
答案 1 :(得分:1)
最后,我用另一种方式弄清楚了。 active_for_authentication?
和valid_for_authentication
已删除。
然后在app/models/user.rb
中,我根据https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address#overwrite-devises-find_for_database_authentication-method-in-user-model定义了以下代码
def self.find_for_database_authentication(warden_conditions)
if valid_for_database_authentication?(warden_conditions)
super
else
throw :warden, message: not_allowed_for_authentication
end
end
# Check wheter user was invited or not
private_class_method def self.valid_for_database_authentication?(params)
User.invitation_accepted.find_by(params).present?
end
private_class_method def self.not_allowed_for_authentication
I18n.t('devise.failure.user.not_allowed_for_authentication')
end