Unable to develop non-waste work

I need to access the Google API, I'm trying to get users to log in to their google account using Devise 2.1.1 and Omniauth (using gem google_oauth2 , since OAuth2 is recommended from Google docs).

Unfortunately, I can't get it to work, it's routes.rb

 TestApp::Application.routes.draw do root :to => 'Landing#index' devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } devise_scope :user do get 'sign_in', :to => 'users/sessions#new', :as => :new_session get 'sign_out', :to => 'users/sessions#destroy', :as => :destroy_session end end 

Then I configured config/initializers/devise.rb to include a line with

  config.omniauth :google_oauth2, 'ID', 'SECRET', {access_type: 'offline', approval_prompt: 'force', scope: 'https://www.googleapis.com/auth/analytics.readonly'} 

And actually I have a void callback class app/controllers/users/omniauth_callbacks_controller.rb

 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController end 

When I click "Sign in to Google," I redirect to Google asking for permission, then return to the callback, and I redirect to the URL http://localhost:3000/sign_in.user with an error

 uninitialized constant Users::SessionsController 

What is wrong here? I spent 2 days trying to figure it out myself, I played with a lot of configurations, but never worked, it disappointed me.

This is the result of rake routes

 root / Landing#index user_omniauth_authorize /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/google_oauth2/} user_omniauth_callback /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:google_oauth2) new_session GET /sign_in(.:format) users/sessions#new destroy_session GET /sign_out(.:format) users/sessions#destroy 
+4
source share
1 answer

It seems you are redirected to your sign_in route, which is looking for users/sessions#new for your routes.rb .

However, Users::SessionsController does not exist, which leads to the described error.

I'm not sure in which scenarios Devise / OmniAuth redirects you to this particular route, but I think you can either go to your home page (where people can click the Google OAuth button on the button / link again) or connect it directly on the Google page OAuth (This can cause people to get stuck in an infinite loop depending on how Devise / OmniAuth uses the sign_in route.)

+1
source

All Articles