Facebook 認証をデバイスの Customer モデルに追加しようとしています。ログイン リンクは Facebook アプリのリンクにリダイレクトされますが、ログインをクリックするとサインアップ ページにリダイレクトされ、顧客はデータベースにまったく保持されません。顧客が保存されていない理由と、顧客を顧客ショーページにリダイレクトする方法について、誰かが洞察を与えることができますか? 前もって感謝します:
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 ファイル (私のモデル):
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