title: Authenticated routes with Omniauth Google
body: <p>I'm trying to make an authenticated route <code>/me</code> using <a href="https://github.com/zquestz/omniauth-google-oauth2" rel="nofollow noreferrer">omniauth-google-oauth2</a>. So far, I have the library set up to login and logout and it's working fine. However, I want to have certain routes be accessible only when a user is logged in. I found <a href="https://stackoverflow.com/questions/4477745/omniauth-before-filter-for-requiring-login">this snippet</a> and made some small changes to fit my set up.</p>
  
  <p><em>application_controller.rb</em></p>
  
  <pre><code>before_filter :authenticate
  def authenticate
      redirect_to :login unless User.from_omniauth(env["omniauth.auth"])
  end
  </code></pre>
  
  <p><em>user.rb</em></p>
  
  <pre><code>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
  </code></pre>
  
  <p>I used <code>env["omniauth"]</code> because that's the auth hash I used in my <code>SessionsController</code>. </p>
  
  <p>However, now whenever I go to <code>localhost:3000</code>, I get the following error:</p>
  
  <pre><code>undefined method `provider' for nil:NilClass
  </code></pre>
  
  <p>I'm assuming this happens because <code>env["omniauth.auth"]</code> is not accessible from <code>application_controller.rb</code>? If that's the case, then how do I properly access the auth hash?</p>