使用Omniauth Google进行身份验证的路线

时间:2016-12-29 00:10:28

标签: ruby-on-rails ruby authentication omniauth omniauth-google-oauth2

我尝试使用omniauth-google-oauth2制作经过身份验证的路线/me。到目前为止,我已经设置了登录和注销的库,它工作正常。但是,我希望只有在用户登录时才能访问某些路由。我找到this snippet并做了一些小改动以适合我的设置。

application_controller.rb

before_filter :authenticate
def authenticate
    redirect_to :login unless User.from_omniauth(env["omniauth.auth"])
end

user.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中使用了auth哈希。

但是,现在每当我转到localhost:3000时,都会收到以下错误:

undefined method `provider' for nil:NilClass

我假设发生这种情况是因为env["omniauth.auth"]无法访问application_controller.rb?如果是这种情况,那么我该如何正确访问auth哈希?

1 个答案:

答案 0 :(得分: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来查找当前用户。

相关问题