So this code (Devise & OAuth2 in Rails 5)
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
puts request.env['omniauth.auth']
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect root_path, event: :authentication # <--- THIS LINE IS THE CULPRIT
else
redirect_to root_path, flash: { error: 'Authentication failed!' }
end
end
end
tells me
undefined method `router_name' for nil:NilClass
How do I go about tracking this down?
What exactly is it that is nil at this stage?
I got no enlightenment doing the following...
and the server log...
Started GET "/users/auth/google_oauth2" for ::1 at 2016-07-22 19:06:28 -0400
I, [2016-07-22T19:06:28.730884 #5714] INFO -- omniauth: (google_oauth2) Request phase initiated.
Started GET "/users/auth/google_oauth2/callback?state=45315985daa3339fe9fa10f3e57dedaadfc4f4aa60f06f06&code=4/I8O7qeJSR--qeDdep-Iwf1EkdI--SlAj2KWJz7MGCpE" for ::1 at 2016-07-22 19:06:34 -0400
I, [2016-07-22T19:06:34.760625 #5714] INFO -- omniauth: (google_oauth2) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#google_oauth2 as HTML
Parameters: {"state"=>"45315985daa3339fe9fa10f3e57dedaadfc4f4aa60f06f06", "code"=>"4/I8O7qeJSR--qeDdep-Iwf1EkdI--SlAj2KWJz7MGCpE"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT $3 [["provider", "google_oauth2"], ["uid", "106038339500381304171"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 17ms (ActiveRecord: 4.5ms)
NoMethodError (undefined method `router_name' for nil:NilClass):
app/controllers/users/omniauth_callbacks_controller.rb:6:in `google_oauth2'
Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (10.3ms)
Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms)
Rendering /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
Rendered /Users/davidwilbanks/.rvm/gems/ruby-2.3.1@global/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (98.5ms)
Just change sign_in_and_redirect root_path
with sign_in(:user, @user)
.
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
puts request.env['omniauth.auth']
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in(:user, @user), event: :authentication
else
sign_in(:user, @user), flash: { error: 'Authentication failed!' }
end
end
end
hope this help you .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With