0

我正在尝试/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? 如果是这种情况,那么我该如何正确访问身份验证哈希?

4

1 回答 1

1

尝试这个:

application_controller.rb

before_filter :authenticate

def authenticate
  redirect_to :login unless user_signed_in?
end

def user_signed_in?
  !!current_user
end

def current_user
  @current_user ||= begin
    User.find(session[:current_user_id]) || fetch_user_from_omniauth
  end
end

def fetch_user_from_omniauth
  user = User.from_omniauth(env['omniauth.auth'])
  session[:current_user_id] = user.id
  user
end

这将首先尝试获取已经登录的用户(从会话)。如果没有找到,它会尝试从omniauth创建一个用户,然后在session中设置它的id,这样对于下一个请求,它不需要在env中的omniauth来找到当前用户。

于 2016-12-29T05:56:53.457 回答