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 inthis 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" %>
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 %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.