I have been trying to add facebook authentication to my devise Customer model. While the login link does redirect to the facebook app link, after clicking login it redirects me to my sign up page, and the customer is not persisted in my database at all. Can someone please shed some insight as to why my customers aren't being saved and how I can redirect them to the customer show page? Thanks in advance:
Omniauth.rb:
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, '*********', '**********'
end
omniauth_callbacks_controller.rb:
class Customers::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@customer = Customer.from_omniauth(request.env["omniauth.auth"])
if @customer.persisted?
sign_in_and_redirect @customer, :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_customer_registration_url
end
end
end
Customer.rb file (my model):
class Customer < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
validates :customername, presence: true
has_many :orders
has_many :pins, :dependent => :destroy
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |customer|
customer.email = auth.info.email
customer.password = Devise.friendly_token[0,20]
customer.customername = auth.info.name # assuming the user model has a name
end
end
end