The action 'google' not found for Users::OmniauthCallbacksController - ruby-on-rails-3

i'm using omniauth-google for login with gmail. It is give me an error action "google" couldn't be found. while i have define it in "Users::OmniauthCallbacksController" like below.
def google
end
this code in my route file
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks"
}
what is the problem i don't understand?
can you Please help?
link sign in with google go here "localhost:3000/users/auth/google". Then i have grant access like this in screenshot.

Use google_oauth2 instead of google oauth.
Gem
gem 'omniauth-google-oauth2'
devise.rb
config.omniauth :google_oauth2, "APP_ID", "APP_SECRET"
OmniauthCallbacksController
def google_oauth2

Related

How do I redirect devise sign_in path to a configured URL?

In a rails 3 project with devise, how puts the login page /users/sign_in in the root path.
my application is placed in
URL like: http://host/my_app
I am using devise for authentication
when i call the URL, the URL should automatically redirect to
http://host/my_app/users/sign_in
but
my application is forwarding to
http://host/users/sign_in
how to redirect the devise authentication to http://host/my_app/users/sign_in?
Thanks in advance
The devise docs talk about how to update the sign-in and sign-out routes. You should be able to prepend my_app using the same procedure documented.
you can skip default routes, check it out here
# config/routes.rb
devise_for :users, :skip => [:sessions]
as :user do
get 'my_app/users/sign_in' => 'devise/sessions#new', :as => :new_user_session
post 'my_app/users/sign_in' => 'devise/sessions#create', :as => :user_session
delete 'my_app/users/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

Devise Omniauth-facebook No route matches [GET] "/users/auth/facebook/callback"

I got a question will trying to use omniauth-facebook with devise
I followed this railscast video(just substitute twitter with facebook) and was able to get it work.
However, I would like to customize my routes a little bit, so this is what I did:
config/routes.rb
devise_for :users, path: 'accounts',
controllers: { omniauth_callbacks: "omniauth_callbacks" },
path_names: { sign_in: "login", sign_out: "logout", sign_up: "signup" }
app/controllers/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Successfully signed in via #{request.env["omniauth.auth"].provider}!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :facebook, :all
end
as I expected, in my url "users/..." was changed to "accounts/...", but when I tried to login via facebook, I got this error:
Routing Error
No route matches [GET] "/users/auth/facebook/callback"
after play around with it, I got around it by adding this line:
config/routes.rb
get '/users/auth/facebook/callback', to: redirect('/accounts/auth/facebook/callback')
It works fine now, but I think there must be better solution.
Is there any way to change the returned callback url from /users/auth/facebook/callback to /accounts/auth/facebook/callback ???
Thanks!
Problems Solved, I forgot to set window.location in Javascript code.
After I changed my window.location, it's working fine

The action 'google_oauth2' could not be found for Users::OmniauthCallbacksController

I'm using 'omniauth-google-oauth2' for sign in with google and follow all instruction here carefully
https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview
but i have error above.
my routes
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks"
}
devise.rb code
config.omniauth :google_oauth2, "863625299460- 420n6c7lvad91dfvko60uamtvtr6huhf.apps.googleusercontent.com", "dcvA2aZRZi27KCQjWTYP30pw", { access_type: "offline", approval_prompt: "" }
omniauth callback controller code
def google_oauth2
##user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
binding.pry #control not coming here
end
i have error below after callback. see screenshot
https://github.com/zquestz/omniauth-google-oauth2/issues/52
This looks like a route issue. If you do "rake routes | grep auth" what do you see?
I had exactly the same problem you described. Make sure you require the omniauth-google-oauth2 gem in config/initializers/deviser.rb
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
require "omniauth-google-oauth2"
config.omniauth :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"],
{ access_type: "offline", approval_prompt: "force" }
I've added the entire portion of my devise.rb file to provide context.
It's very late but this answer might be useful for others
If you are using devise for authentication then devise by default generates routes in the route file devise_for :users and your omniouth_callback route should be above the default devise route so that it overwrites default devise route.
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks"}
devise_for :users

Devise and Facebook Custom Routing

I am trying to create the user registration views and model on my website but I am having a small issue :
I am using devise and omniauth to get the facebook connect features working and it works,
But I want my facebook users when they sign in the first time to create their password,
That is also working, I redirect them to the filled sign up form and they only have to enter their password. But I want them to go to a second "sign_up form" named /views/registrations/new_facebook.html.erb where they can only enter their password and I will also add some other information,
I created the correct view and tested it but I have no idea how to create the correct routes to bypass Devise default
match '/facebook' => 'registrations#new', :as => 'new_facebook_user_registration'
I believe the issue is with match because that's what's not recognised,
If anyone can help me that would be great thanks,
I added my controller code for omniauth :
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash[:success] = "Welcome back"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_facebook_user_registration_url
end
end
alias_method :facebook, :all
end
How can I make the redirect_to new_facebook_user_registration_url actually work ?
devise_scope :user do
match "registrations/new_facebook" => "registrations#new_facebook"
end
That's the solution I copied in the registrations controller the new method and named it new_facebook and now everything is working as expected !
I think the issue is that you're not overriding the devise method that redirects to that path. Also according to the devise docs your routes should be set up with a "devise_for" call.
Here's the wiki page describing how to do what you are asking to do, although you may need a bit of custom logic to deal with cases that aren't facebook signups.
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
Some example code from that page:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path'
end
end
and the one for routes:
devise_for :users, :controllers => { :registrations => "registrations" }

devise/omniauth - The action 'facebook' could not be found

I'm trying to implement facebook authentication in my app following this guide
I've followed all the steps but get the following error after hitting login.
Unknown action
The action 'facebook' could not be found for Devise::OmniauthCallbacksController
I've created the file omniauth_callbacks_controller in controllers/users. It has a facebook method defined. Any idea how I should debug?
Adding my routes file -
Myapp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
devise_for :users do
resources :posts
end
root :to => 'static_pages#home'
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
end
If you look at the guide it specifies this line for your routes file:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
where you have:
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
see the difference?
I am assuming that your users will be able to login and logout, edit profile, register with facebook or register with email and also you may add Confirmable to devise if you want. You should have extra columns in users table. Something like adding extra fields into your user model first.
rails g migration AddFieledsToUser provider:string uid:string image:string
then run rails db:migrate
Check your users table ensure you have these 3 columns above
Also I am assuming that you you have correctly configured initializers/devise.rb like so: config.omniauth :facebook, 'APP_ID', 'APP_SECRET_KEY', scope: 'email', info_fields: 'email, name' after you've properly created the facebook app. Also assuming you have properly created and configured your omniauth_callbacks_controller.rb based on the gems gem 'omniauth', '~> 1.6' and gem 'omniauth-facebook', '~> 4.0' in your gem file successfully. Just make sure you have all these steps done.
In your routes.rb you can add this:
devise_for :users,
path: '',
path_names: {
sign_in: 'login',
sign_out: 'logout',
edit: 'profile',
sign_up: 'registration'
},
controllers: {
omniauth_callbacks: 'omniauth_callbacks',
}
I think this is the part you have missed. You can also name the routes whatever you want. Just saying.
I ran into a similar problem with tutorials. Check the capitalization of
F in facebook in users/omniauth_callbacks_controller.rb I was using a capital "Facebook" but it was looking for lowercase "facebook"