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
Related
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
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
I'm working on a Rails-based API. I recently started attempting to version it. (I'm using the Versionist gem, in case it matters) One version ('v2') uses Devise and Omniauth to authenticate users through Facebook/Twitter.
I want all the routes associated with this version to have the appropriate version prefix (so users/:username/foo becomes v2/users/:username/foo, etc.), but I've already found out that putting devise_for inside the api_version block prevents the Devise helpers (current_user, user_signed_in?, etc.) from working, so it continues to live outside the block:
routes.rb:
devise_for :user, :path => '', :controllers => {:omniauth_callbacks => 'users/omniauth_callbacks'}, :skip => [:registrations, :confirmations, :sessions, :passwords]
api_version(:module => "V2", :path=>"v2") do
resources :authentications, :only => [:update, :destroy]
devise_scope :user do
post 'login' => 'sessions#create', :as => 'user_session'
get 'logout' => 'sessions#destroy'
post 'password' => 'devise/passwords#create'
put 'password' => 'devise/passwords#update'
end
end
Everything seemed great... except the Devise-generated omniauth routes:
rake routes output:
user_omniauth_authorize /auth/:provider(.:format)
user_omniauth_callback /auth/:action/callback(.:format)
Now, some google-fu revealed that there's a devise configuration setting for this, so I added the following to our devise initializer (config/initializers/devise.rb):
Devise.setup do |config|
config.omniauth_path_prefix = 'v2/auth'
end
Now, rake routes produces paths that look sensible:
user_omniauth_authorize /v2/auth/:provider(.:format) v2/users/omniauth_callbacks#passthru {:provider=>/(?!)/}
user_omniauth_callback /v2/auth/:action/callback(.:format) v2/users/omniauth_callbacks#(?-mix:(?!))
However, when I attempt to access this route by calling api.localhost/v2/auth/facebook, I get a routing error:
ActionController::RoutingError (No route matches [GET] "/v2/auth/facebook")
Any idea what's going on here?
You are missing the provider name in the routes so they don't match the facebook part in /v2/auth/facebook. The correct route destination should look something like v2/users/omniauth_callbacks#(?-mix:facebook).
Have you specified the provider in the user model?
devise_for ..., :omniauthable, :omniauth_providers => [:facebook]
For the record, I'm using Rails 3.2 and Devise 3.0 and the altered route seems to work (I haven't gone further yet to see if something else will break).
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"
I've read similar articles like Override devise registrations controller , but Devise in my app doesn't override correctly.
I am using Devise 1.4.2 and Rails 3.0.9 on my Mac (OSX 10.6.8).
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
debugger
end
def create
debugger
end
end
# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Then I started my local server with --debugger option (ruby-debug19 is installed) and when access to /users/sign_up, debugger doesn't trigger.
rails server --debugger
Started GET "/users/sign_up" for 127.0.0.1 at 2011-07-11 21:21:46 +0900
Processing by Devise::RegistrationsController#new as HTML
Rendered devise/shared/_links.erb (3.8ms)
Rendered devise/registrations/new.html.erb within layouts/application (20.4ms)
Updated: I checked my routings with "rake routes", but the routing for sign_up still remains as below.
user_registration POST /users(.:format) {
:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {
:action=>"new", :controller=>"devise/registrations"}
I want :controller to point "registrations" (without "devise/") when POST.
What am I missing?
In your routes.rb file,
You can amend your devise routes in the following way:
devise_for :users, :controllers => { :registrations => 'registrations' }