我们的应用程序使用 Devise 进行注册,并与用户模型和配置文件模型完美配合。
将表单中的注册信息保存到 Users 表中,并使用其他信息在 SQL 上的 Profiles 表中创建配置文件并嵌套 user_id。
Omniauth Facebook 正在努力将内容保存到 User 模型,但我想像 Devise 注册那样将从 Facebook Auth 获取的名称和用户名保存到 Profile 模型。
TABLE 用户
| id | email | encrypted_password | reset_password_token | reset_password_sent_at | remember_created_at | sign_in_count | current_sign_in_at | last_sign_in_at | current_sign_in_ip | last_sign_in_ip | created_at | updated_at | role_id | provider | uid |
表格配置文件
| id | user_id | username | name | lastname | gender | birthday | created_at | updated_at |
MODEL User.rb
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy, autosave: true
accepts_nested_attributes_for :profile
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]
belongs_to :role
before_create :set_default_role
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
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
private
def set_default_role
self.role ||= Role.find_by_name('registered')
end
end
MODEL Profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
validates :username, presence: true, length: {maximum: 255}, uniqueness: { case_sensitive: false }, format: { with: /\A[a-zA-Z0-9]*\z/, message: "deve contar apenas letras e números" }
validates :name, presence: true, length: {maximum: 255}
validates :lastname, presence: true, length: {maximum: 255}
validates :gender, presence: true, inclusion: %w(m f)
validates :birthday, presence: true
end
CONTROLLER application_controller.rb(处理自定义字段的权限)
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :provider, :uid, profile_attributes: [:name, :lastname, :username, :birthday, :gender, :id]) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me, :provider, :uid) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
end
任何人都知道我怎样才能让 Omniauth Facebook 像 Devise 注册一样运行以创建用户配置文件,集成用户和配置文件这两个模型而不破坏 Devise 注册:P?
谢谢你:)
最佳答案
我不知道 facebook 注册或登录您的应用程序的流程,所以我只是假设当用户点击登录或注册 facebook 时,您的应用程序基本上从 facebook 获取电子邮件并创建仅当该电子邮件不存在于数据库中时才创建新用户,否则它只是让他们登录。
要实现这一点,您需要覆盖您的 find_for_facebook_oauth(auth)
方法。你可以在你的方法中做这样的事情
def self.find_for_facebook_oauth(auth)
if user = User.find_by_email(auth.info.email) # search your db for a user with email coming from fb
return user #returns the user so you can sign him/her in
else
user = User.create(provider: auth.provider, # Create a new user if a user with same email not present
uid: auth.uid,
email: auth.info.email,
password: Devise.friendly_token[0,20])
user.create_profile(name: auth.info.name, # you need to check how to access these attributes from auth hash by using a debugger or pry
#your other profile attributes
)
return user
end
end
然后你的回调方法将是相同的
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #make sure user is confirmed else it'll throw an error
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
end
如果您的流程类似于 Yahoo,您使用来自 facebook 的数据呈现带有自动填充字段的表单,那么您无需创建,只需在 user
和 profile
中初始化 find_for_facebook_oauth(auth)
带有语句的方法
user = User.new(#user attributes)
user.build_profile(#profile attributes)
return user # and then return initialised user
由于用户不在数据库中,它将在 facebook 回调方法中执行 else block ,并呈现带有自动填充字段的登录页面。
关于ruby-on-rails - 使用模型用户和配置文件 + Omniauth Facebook 进行设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105369/