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.
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" %>