首页 > 解决方案 > Facebook 在验证用户 Rails 后重定向到注册页面

问题描述

我正在使用设备集成 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

我真的很感激能多关注我做错了什么,或者我错过了什么。谢谢!

标签: facebookdeviseruby-on-rails-5facebook-login

解决方案


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

rails g migration add_provider_and_uid_to_users provider:string uid: string

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


推荐阅读