我使用goise和facebook进行用户身份验证的omise omnia。谷歌工作得很好,但Facebook被困在重定向页面,虽然在他们的平台上用户正确登录(在Facebook上)。
这是我得到的日志:
2014-09-01T15:26:41.996884+00:00 app[web.1]: (facebook) Request phase initiated.
2014-09-01T15:26:42.211524+00:00 app[web.1]: (facebook) Request phase initiated.
2014-09-01T15:26:41.994627+00:00 app[web.1]: Started GET "/users/auth/facebook?locale=es" for 190.15.201.45 at 2014-09-01 15:26:41 +0000
2014-09-01T15:26:42.205674+00:00 app[web.1]: Started GET "/users/auth/facebook?locale=es" for 190.15.201.45 at 2014-09-01 15:26:42 +0000
2014-09-01T15:26:42.217355+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook?locale=es" host=myapp.herokuapp.com request_id=2b9aab45-c511-4ac9-b36f-4e6925cba3aa fwd="190.15.201.45" dyno=web.1 connect=2ms service=16ms status=302 bytes=1284
我的路线:
devise_for :users, :controllers => { omniauth_callbacks: "users/omniauth_callbacks" }
我的omniauth_callbacks_controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
@user = User.find_for_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
}
end
[:google_oauth2, :facebook].each do |provider|
provides_callback_for provider
end
我的用户模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :confirmable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
belongs_to :company
before_save :create_user_company
def create_user_company
if self.company_id.nil?
company = Company.new
company.email = self.email
company.save
self.company_id = company.id
self.admin = true
end
end
def self.find_for_oauth(auth, signed_in_resource = nil)
identity = Identity.find_for_oauth(auth)
user = signed_in_resource ? signed_in_resource : identity.user
if user.nil?
email = auth.info.email
user = User.where(:email => email).first if email
if user.nil?
user = User.new(
name: auth.extra.raw_info.name,
email: email ? email : "temp_email@mail.com",
password: Devise.friendly_token[0,20]
)
user.skip_confirmation!
user.save!
end
end
if identity.user != user
identity.user = user
identity.save!
end
user
end
end
设计初始化程序是基本的:
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET']
但我也试过this(没有用):
config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'],{client_options: {ssl: {ca_file: Rails.root.join('lib/assets/cacert.pem').to_s}}}
我正在使用这个宝石:
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
gem 'devise'
gem 'figaro' #for safe saving of env vars
我很感激你的暗示。 提前谢谢。
PS:this不是答案
答案 0 :(得分:0)
好的,如果有人遇到同样的问题,这就解决了这个问题: