lynndylanhurley / devise_token_auth

Token based authentication for Rails JSON APIs. Designed to work with jToker and ng-token-auth.
Do What The F*ck You Want To Public License
3.54k stars 1.14k forks source link

undefined method `[]' for nil:NilClass during omniauth callback #682

Closed franciscoml67 closed 7 years ago

franciscoml67 commented 8 years ago

I think there's some kind of problem with this line of code in omniauth_callbacks_controller (line 14):

...

  devise_mapping = [request.env['omniauth.params']['namespace_name'],
                    request.env['omniauth.params']['resource_class'].underscore.gsub('/', '_')].compact.join('_')
...

After successfully being prompted by facebook, the 'omniauth' object seems to be empty raising the following error:

undefined method `[]' for nil:NilClass

C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/bundler/gems/devise_token_auth-162967970c95/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb:14:in `redirect_callbacks'

I think this issue has been addressed before, but I think it wasn't solved and it has now, resurfaced.

franciscoml67 commented 8 years ago

A more detailed description of what is going on can be found here

franciscoml67 commented 8 years ago

update: the request.env['omniauth.params'] is coming in null from the callback, as well as request.env['omniauth.auth'] for that matter...

would really appreciate some help in this @lynndylanhurley ! thank you in advance

jaredcnance commented 7 years ago

I'm also running into this issue. In my case, I am trying to perform hybrid auth with Google from an Ember app.

The Ember app posts the authorization here:

// routes.rb
post '/auth/:provider/callback', to: 'sessions#create'

which is handled by this controller:

class SessionsController < ApplicationController
  def create
    @account = Account.from_omniauth(request.env['omniauth.auth'])
    session[:account_id] = @account.id

    render json: { account: @account,
      access_token: request.env['omniauth.auth']['credentials']['token'],
      email: request.env['omniauth.auth']['info']['email']  }
  end
end

with the initializer:

// initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  def google_oauth2_options
    {
      :provider_ignores_state => true,
      :prompt => 'select_account consent',
      :access_type => 'offline',
      :scope => "email, profile, plus.me",
      :redirect_uri => "http://localhost:4200/oauth2callback"
    }
  end

  provider :google_oauth2,  ENV['GOOGLE_CLIENT_ID'],   ENV['GOOGLE_CLIENT_SECRET'], google_oauth2_options

end

Now, if I use just the omniauth package by itself, everything work fine. If I add this to the controller:

puts puts request.env['omniauth.auth'].to_yaml

I get an empty response using this library. If I use the omniauth-google-oauth2 gem by itself, I get:

ruby/hash:OmniAuth::AuthHash
provider: google_oauth2
uid: ...
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
 ...
zachfeldman commented 7 years ago

Hi there @franciscoml67 ,

In an effort to cleanup this project and prioritize a bit, we're marking issues that haven't had any activity in a while with a "close-in-7-days" label. If we don't hear from you in about a week, we'll be closing this issue. Obviously feel free to re-open it at any time if it's the right time or this was done in error!

If you are still having the issue (especially if it's a bug report) please refer to our new Issue Template to provide some more details to help us solve it.

Hope all is well.

mgs96 commented 6 years ago

hey @franciscoml67 @zachfeldman I'm having this same issue!

fabien7337 commented 4 years ago

hey @franciscoml67 @zachfeldman I'm having this same issue!

DumasOlivier commented 3 years ago

@franciscoml67 did you find any solution ? Getting the same problem ...

fabien7337 commented 3 years ago

Yes, I stopped using devise_token_auth and omniauth :)

I did it from scratch by myself. It is not this complicated and at least you have no issue!

DumasOlivier commented 3 years ago

Yes, I stopped using devise_token_auth and omniauth :)

I did it from scratch by myself. It is not this complicated and at least you have no issue!

Ahaha maybe I will do the same in a few hours 🤯🤯🤯

DaniyalBasit commented 5 months ago

Trying a different version of omniauth-google-oauth2 gem solved the issue for me.

sumitmaurya021 commented 2 months ago

Error during Google OAuth callback: undefined method `persisted?' for nil:NilClass

how to solve this problem?