【发布时间】:2017-12-08 11:27:35
【问题描述】:
我从过去 2 天开始尝试解决这个问题,但直到现在都无济于事。我正在使用 rails 中的omniauth,并且我已经在我的站点中集成了omniauth 身份验证,但我想为所有提供者创建单独的表。我有两个表1:用户和另一个是auth_provider 1:用户将包含用户数据 2:auth_provder 将包含带有社交媒体提供商身份验证详细信息的 user_id。
我已经创建了从用户到 auth_providers 的 has_many 关联。
现在我想在两个表中插入数据,以便首先在用户表中创建带有一些字段的条目,然后它应该采用新插入的记录条目并将此 id 与其他数据一起插入到 auth_providers 表中。我知道这件事是可能的,但我找不到任何示例或代码。我只是从过去 2 天开始搜索和尝试,但仍然没有成功。我想使用关联来做到这一点,我也使用了nested_attributes,但仍然没有成功。
这是我的代码
用户模型:
class User < ApplicationRecord
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable,
:omniauthable, :omniauth_providers => [:facebook,:twitter,:linkedin]
has_many :organizations_users
has_many :auth_providers
has_many :organizations, through: :organizations_users
accepts_nested_attributes_for :auth_providers
def active_for_authentication?
# Uncomment the below debug statement to view the properties of the returned self model values.
super && self.active && self.exp_alert == false
end
def self.from_omniauth(auth)
exist = where(email: auth.info.email).first
if exist
existing_user = exist["id"]
Auth_provider.where(provider: auth.provider, social_uid: auth.uid).first_or_create do |auth_provider|
auth_provider.provider = auth.provider
auth_provider.social_uid = auth.uid
auth_provider.social_token = auth.credentials.token
auth_provider.user_id = existing_user
end
else
#@organization.users.create(first_name:params[:first_name])
names = auth.info.name.strip.split(" ")
first_name = names[0]
last_name = names[1]
params = { user: {
email: 'abc@abc.com', auth_providers_attributes: [
{ provider: 'facebook' },
{ social_token: 'asfasf2342432' },
{ social_uid: 'asfdaf23242'} # this will be ignored
]
}}
user = User.create(params[:user])
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.find_or_create_from_auth_hash(auth_hash)
user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
user.first_name = auth_hash.info.nickname
user.active = 'true'
user.admin=='false'
user.exp_alert = 'false'
user.password = Devise.friendly_token[0,20]
user.token = auth_hash.credentials.token
user.email = auth_hash.info.email
user.secret = auth_hash.credentials.secret
user.skip_confirmation!
end
user
end
def self.linkedin_hash(auth_hash)
user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
user.first_name = auth_hash.info.first_name
user.last_name = auth_hash.info.last_name
user.active = 'true'
user.admin = 'false'
user.exp_alert = 'false'
user.password = Devise.friendly_token[0,20]
user.token = auth_hash.credentials.token
user.email = auth_hash.info.email
user.skip_confirmation!
end
user
end
def inactive_message
"Your Account has not been active yet."
end
def after_confirmation
super
self.update_attribute(:active, true)
end
end
auth_providers 模型
class Auth_provider < ApplicationRecord
devise :database_authenticatable
belongs_to :user
accepts_nested_attributes_for :user
end
Facebook 的回复是这样的
#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1517924507 token="EAAdDsC4F0CCLq95eo81qfaMVVs0zeNgqtRqUF9ufZB3gK609NY3aiikJ9AvE8zSB63WFcG0E6NBFNIWf00DjgHNlsQHCd2D26uxJ1ongQ5YBJZCeuZAOas2SEYlRwPYhctfiEVVdbadOyA3QeL50JHIA5dKa3xdfK5Efw9Y"> extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash email="testing@gmail.com" id="35232364989259" name="John">> info=#<OmniAuth::AuthHash::InfoHash email="testing@gmail.com" image="http://graph.facebook.com/v2.6/35489/picture" name="John"> provider="facebook" uid="354814524989259">
【问题讨论】:
-
你会先从任何控制器创建用户吗?
标签: ruby-on-rails ruby devise omniauth