Devise Wrong number of arguments (13 for 2) upon Omniauthable - ruby-on-rails

Trying to follow this tutorial to implement Google Sign In With Devise into my rails app: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
Getting this error:
My routes:
devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations', omniauth_callbacks: 'users/omniauth_callbacks' }
Omniauthable controller: app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
puts #user
if #user.persisted?
sign_in_and_redirect #user
set_flash_message(:notice, :success, kind: "Google") if is_navigational_format?
else
puts "WENT INTO HERE---------"
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to root_path
end
end
def failure
redirect_to root_path
end
end
User.rb
class User < ApplicationRecord
devise :rememberable, :trackable,
:omniauthable, omniauth_providers: %i[google_oauth2]
# rubocop:disable Metrics/AbcSize
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
# user.password = Devise.friendly_token[0,20]
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.image_url = auth.info.image # assuming the user model has an image
end
end
end
I have identified the 13 arguments that the app is referring to, from the session dump:
warden.user.user.key: {"id"=>204, "uid"=>"*******", "first_name"=>"First name", "last_name"=>"last name", "provider"=>"google_oauth2", "image_url"=>"*****", "email"=>"***", "has_logged_in"=>false, "created_at"=>"2018-03-17T21:26:24.782Z", "updated_at"=>"2018-03-17T23:31:38.220Z", "admin"=>false}
But I don't know where this came from or how to solve it

Not sure what the issue was but after closing my browser, the problem was fixed. So it probably had something to do with my cache.

Related

Omniauth2 Google Record Invalid Error using Devise

I'm using Devise with Google Omniauth, but I keep getting User Record Invalid error:
I only have this validation in my User model:
validates :email, presence: true, uniqueness: true
User model
devise :omniauthable, omniauth_providers: [:google_oauth2]
def self.from_google(email:, full_name:, uid:, avatar_url:)
create_with(uid: uid, full_name: full_name, avatar_url: avatar_url).find_or_create_by!(email: email)
end
My routes
devise_for :users, :controllers => { omniauth_callbacks: 'users/omniauth_callbacks', :registrations => "registrations"}
omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
user = User.from_google(from_google_params)
if user.present?
sign_out_all_scopes
flash[:success] = t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect user, event: :authentication
else
flash[:alert] = t 'devise.omniauth_callbacks.failure', kind: 'Google', reason: "#{auth.info.email} is not authorized."
redirect_to new_user_session_path
end
end
protected
def after_omniauth_failure_path_for(_scope)
new_user_session_path
end
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || root_path
end
private
def from_google_params
#from_google_params ||= {
uid: auth.uid,
email: auth.info.email,
full_name: auth.info.name,
avatar_url: auth.info.image
}
end
def auth
#auth ||= request.env['omniauth.auth']
end
end
Sessions controller
class Users::SessionsController < Devise::SessionsController
def after_sign_out_path_for(_resource_or_scope)
new_user_session_path
end
def after_sign_in_path_for(resource_or_scope)
stored_location_for(resource_or_scope) || root_path
end
end
Any clue on how to trace or fix this? It's driving me crazy! Thanks
Devise tries to display a relevant error message for the Spanish-translated app of your site. You are missing a translation for the Spanish error message.
Go to config/locales/es.yml and add in:
es:
activerecord:
errors:
messages:
record_invalid:
"Spanish error message comes here"

Omniauth redirects back to signin page

So I'm trying to integrate Google OAuth2 with devise in my app but after signing in and allowing access to the app I get redirected back to the sign in page for some reason. I'm using the following tutorial:
https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application
Here are my files:
callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
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
route.rb:
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
users.rb:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
devise.rb:
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], callback_url: ENV['GOOGLE_CALLBACK_URL']
What am I doing wrong? Thanks
Try this:
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
# return path you want to go to...
end
end

Omniauth-facebook with Devise: "Missing passthru" errors

I have Devise authentication installed with no problems. Now I'm trying to add an option to log in with Facebook, using Omniauth-facebook.
I followed the instructions in this guide, but I'm getting errors about missing "Passthru" documentation, when visiting the url localhost:3000/auth/facebook.
Here's the first error I got:
Unknown action
The action 'passthru' could not be found for RegistrationsController
I tried a bandaid fix by just adding an empty "passthru" action to my controller:
def passthru
end
And that resolved that error, but I got a different one in return:
Template is missing
Missing template registrations/passthru, devise/registrations/passthru, devise/passthru, application/passthru with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/home/user/project/app/views" * "/home/user/.rvm/gems/ruby-2.0.0-p648#railstutorial_rails_4_0/gems/devise-3.5.2/app/views"
I tried creating a "passthru.html.erb" in the stated folders, but that error remained. In any case, I think these errors are emblematic of a deeper problem.
Has anyone else run into this problem? All I could find on it was this SO question, but none of the answers were helpful.
My code so far:
Gemfile
gem 'devise'
gem 'omniauth-facebook'
gem 'omniauth'
routes.rb
devise_for :members, controllers: { registrations: 'registrations', omniauth_callbacks: 'registrations' }
member.rb
devise :database_authenticatable, :registerable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
member.email = auth.info.email
member.password = Devise.friendly_token[0,20]
member.title = auth.info.name
end
end
registrations_controller.rb
def facebook
#member = Member.from_omniauth(request.env["omniauth.auth"])
if #member.persisted?
sign_in_and_redirect #member, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_member_registration_url
end
end
def failure
redirect_to root_path
end
def passthru
end
initializers/devise.rb
config.omniauth :facebook, "<app_id>", "<app_secret>"
Try this :
Update GemFile:
gem 'omniauth-facebook'
gem 'omniauth'
Goto rails_apps/yourapp/config/initializers/devise.rb
Devise.setup do |config|
config.omniauth :facebook, "KEY", "SECRET"
end
Update the User Model
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
end
Goto : rails_apps/yourapp/config/routes.rb
Rails.application.routes.draw do
devise_for :users
resources :users
end
Edit in View
<%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>
Passthru is a relic from omniauth update your gems of devise omniauth and so on .
there is a controller called omiauth_callback this is the one makes noizes ;P.(may help you trace the problem's source)
If you create a method in controller like so :
def passthru
end
You HAVE TO create a view with(even empty), or redirection :get inspired by ajax techniques to bypass html rendering .
Hope it send you on the way of problem solving .
try also theses routes :
```
user_omniauth_authorize /users/auth/:provider(.:format) sessions#passthru {:provider=>/facebook|twitter|google/}
user_omniauth_callback /users/auth/:action/callback(.:format) sessions#(?-mix:facebook|twitter|google)
```
You can create another controller for Omniauth callback
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#user = User.from_omniauth(request.env['omniauth.auth'])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def after_sign_in_path_for(resource)
super resource
end
end
you need to reset your routes as
devise_for :members, :controllers => {:registrations => "members/registrations", :omniauth_callbacks => 'omniauth_callbacks'}
As i could remember you need not to use :omniauth_providers => [:facebook] in your member.rb
Now you can add a button in your sign_up page or instead include the below code in your devise/shared/_links.html.erb, because it will be available in your sign_in form also.
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Sign up with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), class: "btn btn-default navbar-btn" %><br />
<% end -%>
<% end -%>
you also need to configure devise in initializers
In your /config/initializers/devise.rb
config.omniauth :facebook, "App ID", "App Secret", scope: 'email', info_fields: 'email,name'
Please go through this simplest tutorial for Sing_up with facebook Link
Try this .........
config/initializers/devise.rb
config.omniauth :facebook, ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"], { :scope => 'email, offline_access'}
config/routes.rb
devise_for :members, controllers: { registrations: 'registrations', omniauth_callbacks: "omniauth_callbacks" }
app/models/member.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |member|
member.email = auth.info.email
member.password = Devise.friendly_token[0,20]
member.title = auth.info.name
end
end
app/controllers/omniauth_callbacks_controller.rb
skip_before_filter :authenticate_user!
def facebook
p env["omniauth.auth"]
user = User.from_omniauth(env["omniauth.auth"])
if user.persisted?
flash[:notice] = "You are in..!!!"
sign_in_and_redirect(user)
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
def failure
#handle you logic here..
#and delegate to super.
super
end
Hope this will work for you.
I think the problem is always on the button or the anchor, so use this
<%= link_to user_facebook_omniauth_authorize_path, method: :post do %>
login with facebook
<% end %>

ERR_TOO_MANY_REDIRECTS logging in with OmniAuth/Devise/Rails

I'm attempting to add Facebook login to my Rails 4 site following these instructions:
https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
I'm currently using Devise and Pundit for authorization and authentication. Despite having followed the instructions to my best ability, I'm getting an error. When I click my "Login with Facebook" button, a window pops up that asks for email/password, and when I submit that info, I get an error page that reads:
[MyApp.com] page isn’t working
[MyApp.com] redirected you too many times.
Try:
Reloading the page
Clearing your cookies ERR_TOO_MANY_REDIRECTS
It seems like somehow I've introduced a redirect loop, but I don't really understand the data flow, so it's hard to find where I've gone wrong.
Here's my routes.rb:
Rails.application.routes.draw do
get 'home/index'
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", sessions: "sessions" }
resources :movies
root 'home#index'
end
omniauth_callbacks_controller.rb:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
before_filter :authenticate_user, :except => [:new, :create, :destroy]
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
OmniAuth's configuration in config/initializers/devise.rb:
config.omniauth :facebook, '[APP ID]', '[APP SECRET]', callback_url: "https://#{ENV['C9_HOSTNAME']}/users/auth/facebook",
:client_options => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}
My user model (user.rb):
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
And the link in my view (using HAML):
%button
= link_to "Log in with Facebook", user_omniauth_authorize_path(:facebook)
I've just had this exact problem too! After you recieved this error, were you able to reload the page and it had actually logged you in? If so, then that's what I had. The log in worked - but the redirects weren't good.
Have you added in the function after_sign_in_path_for(resource) to the application_controller.rb that redirects the page after login? I had and that's why the redirects were occuring.
My solution was to fixed the if statement to include the right referrer. I just added in || request.referer.include?("google"), of course you may have to change it to account for 'facebook' in the referrer url.
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url || request.referer.include?("google")
super
goals_path
else
stored_location_for(resource) || request.referer || root_path
end
end
Now... if you haven't used that function then my answer will be useless to you. Good luck.

Devise + Omniauth Facebook redirecting to signup

I have followed this guide spot on https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
First round it authenticated me, signed me in, and saved me to db.
I cleared browser history and tested a different user.
It takes new user to facebook page to sign in, and after sign in automatically redirects them to
http://localhost:3000/users/sign_up#_=_
and does not sign them in or save to DB. Any Ideas?
Routes
get "static/home"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users
root 'static#home'
User.rb
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,
:omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
#user.image = auth.info.image # assuming the user model has an image
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
protected
def password_required?
true
end
end
devise.rb
config.omniauth :facebook, "#######", "################"
omniauth_callbacks
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"].except('extra')
redirect_to new_user_registration_url
end
end
end
home.html.erb
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
The code below is redirecting him. Turns out it is only his facebook account. Need to know why his account is doing that...
if #user.persisted?
sign_in_and_redirect #user #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"].except('extra')
redirect_to new_user_registration_url
end
http://localhost:3000/users/sign_up#_=_
I had exactly the same response. I was running the server locally and looking at the logs, it said it couldn't authenticate the client.
Turns out, it was because I had set the ENV keys locally using export FACEBOOK_APP_SECRET='xxxxxxx' which only works if you then run the server from the same tab in your terminal.
Things to try:
If using exports to set the env key - run $bin/rails s from the same tab. Can check if the key has been set by typing in $printenv in the terminal.
Set env key to the bash profile. Clear instructions from osx daily here.
Use dotenv module to manage keys.
All the best.

Resources