我正在尝试/me
使用omniauth-google-oauth2制作经过身份验证的路由。到目前为止,我已将库设置为登录和注销,并且工作正常。但是,我希望只有在用户登录时才能访问某些路由。我找到了这个片段并进行了一些小的更改以适应我的设置。
application_controller.rb
before_filter :authenticate
def authenticate
redirect_to :login unless User.from_omniauth(env["omniauth.auth"])
end
用户.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.picture = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
我使用env["omniauth"]
了因为那是我在我的SessionsController
.
但是,现在每当我去 时localhost:3000
,我都会收到以下错误:
undefined method `provider' for nil:NilClass
我假设发生这种情况是因为env["omniauth.auth"]
无法从application_controller.rb
? 如果是这种情况,那么我该如何正确访问身份验证哈希?