I'm trying to implement a login system that only supports login through a Google Account to access Analytics data.
I cannot get it working with the exception
undefined method `new_session_path' for #<Users::OmniauthCallbacksController:0x8f588b0>
I'm using omniauth
with the omniauth-google-oauth2
strategy.
This is the Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'mysql2'
gem 'haml'
gem 'haml-rails'
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'devise'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platform => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'rspec-rails'
gem 'cucumber-rails'
gem 'factory_girl_rails'
end
This is the config/routes.rb
TestApp::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
devise_scope :user do
get 'sign_in', :to => 'users/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'users/sessions#destroy', :as => :destroy_user_session
end
root :to => "Landing#index"
end
app/model/user.rb
:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
# :database_authenticatable, :registerable
# :recoverable, :rememberable, :trackable, :validatable
devise :omniauthable
# Setup accessible (or protected) attributes for your model
# attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
config/intitializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, 'ID', 'SECRET', :scope => 'https://www.googleapis.com/auth/analytics.readonly'
end
app/controllers/users/omniauth_callback_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
# You need to implement the method below in your model
@user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Can you please help me to understand what's wrong here?
P.S. this is the result I get running rake routes
user_omniauth_callback /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:(?!))
new_user_session GET /sign_in(.:format) users/sessions#new
destroy_user_session GET /sign_out(.:format) users/sessions#destroy
root / Landing#index