在对用户Rails进行身份验证之后,Facebook重定向到注册页面

时间:2018-12-07 09:43:55

标签: facebook devise ruby-on-rails-5 facebook-login

我正在使用设备整合Facebook登录,并在允许Facebook获取您的信息后将其重定向到注册页面。这是我的代码:

class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception
 before_action :authenticate_user!
 before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
 devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :description, :photo ])

 devise_parameter_sanitizer.permit(:account_update, keys: [:username, :first_name, :last_name, :description, :photo ])
end

end

回调控制器

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
 def facebook
  @user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
  sign_in_and_redirect @user, :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_user_registration_url
 end

end

def failure
  redirect_to root_path
end
end

我的用户模型是

def self.new_with_session(params, session)
super.tap do |user|
  if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
    user.email = data["email"] if user.email.blank?
  end
 end
end

 def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  user.email = auth.info.email
  user.password = Devise.friendly_token[0,20]
  user.first_name = auth.info.name   # assuming the user model has a name
  user.image = auth.info.image # assuming the user model has an image
end
end

我非常感谢多加关注我做错了什么,或者我错过了什么地方。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要将此提供者和uid列添加到您的用户表中,我认为rails无法与您的用户匹配:

rails g migration add_provider_and_uid_to_users provider:string uid: string

尝试给我写Rails服务器的响应,它可以帮助您找出错误源