Sushant Kumar
Sushant Kumar

Reputation: 43

request.env['omniauth.oauth'] returns nil

I am using the omniauth-google-oauth2 gem to authorise google accounts, and getting the authorisation code, but getting the request.env['omniauth.auth'] as nil even after upgrading the gem to latest version.

ruby_version: 2.4.9
rails _version: 4.2.8

My Gemfile

gem 'omniauth', '1.9.1'
gem 'omniauth-oauth2', '1.7.0'
gem 'omniauth-google-oauth2', '0.8.0'

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'MY_GOOGLE_CLIENT_ID', 'MY_GOOGLE_SECRET',  {
    scope: 'email, profile',
    access_type: "offline",
    approval_prompt: "force",
    prompt: 'select_account',
    redirect_uri: 'http://localhost:3000/auth/google-oauth2/callback'
  }

end

routes.rb

get 'auth/google_oauth2/callback', to: 'social_accounts#create_account'

socual_accounts_controller.rb

class Api::V1::SocialAccountsController < ApplicationController

def create_account
  request_params = request.env["omniauth.auth"] # gives nil
end

end

And is it possible to get the access_token and refresh_token from authorisation code ? That will also help a lot

Thanks in advance

Upvotes: 1

Views: 538

Answers (1)

Curtis
Curtis

Reputation: 999

I ran across this recently in one of our applications, so for future generations…

Check the version of oauth2 in your Gemfile.lock. Ours had been updated from 1.4.10 to 2.0.5 during a bundle update. We tried upgrading to the latest (v2.0.7) and downgrading to the first tag on 2 (v2.0.0) but no luck.

Locking oauth2 to 1.4.10, solved our issue with request.env["omniauth.auth"] being nil.

There were a lot of changes that went into 2.0.0, so it's hard to say which one broke this.

Upvotes: 0

Related Questions