我一直在尝试将 facebook 身份验证添加到我的设计客户模型中。虽然登录链接确实重定向到 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