facebook - 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
我真的很感激能多关注我做错了什么,或者我错过了什么。谢谢!
解决方案
您需要将此列的提供者和 uid 添加到您的用户表中,我认为 rails 无法匹配您的用户:
rails g migration add_provider_and_uid_to_users provider:string uid: string
并尝试给我写 Rails 服务器响应它可以帮助找出错误源
推荐阅读
- python - Python:用 re.sub 替换列表中的多个特定单词
- powershell - PSRemoting 参数 RunAsAdministrator '参数集无法解析' 错误
- python - 在字典列表中查找和编辑值的最快方法
- .htaccess - 修改 .htaccess 以直接在浏览器中打开 .txt.gz 文件
- django - 从 bitbucket 管道部署到谷歌应用引擎时如何运行 django 迁移?
- java - Spring数据重复键值
- java - Elatsic 缓存 - redis - 写入 ~30-40 K 键后无法连接 - 连接池问题
- azure-devops - Azure DevOps:管道无法使用来自工件的 NuGet 包
- java - 无法使用源选项编译 Java 11 代码
- r - 变异变量以使用管道和用户定义的函数创建是/否