0

我一直在把头撞在墙上。我问了一个类似的问题,以为我有答案,但我没有。现在我了解更多,但仍然完全迷失了。这是我正在尝试做的事情:

  • 创建一个演示应用程序,展示如何使用 Twitter 和 Facebook API 进行跨平台网络分析。
  • 根据电子邮件地址和密码为每个用户设置一个标准登录名。
  • 使用(最好是内置设计功能)誓言通过 facebook 和 twitter 对每个用户进行身份验证,并将他们的令牌和秘密保存在数据库中。
  • 用户单击 facebook/twitter 图标,如果他们已登录(使用电子邮件和密码),则它会检索他们的 facebook/twitter 凭据,将其与应用程序访问凭据打包,然后将其与 api 请求一起发送以查看用户。
  • 我已将此构建用作应用程序库,应用程序设置在 twitter 和 facebook 上,并且身份验证正在运行 - https://github.com/alex-klepa/rails4-bootstrap-devise-cancan-omniauth

似乎在身份模型(用户的 have_many 身份 - 那些是 oauth 策略)中,令牌和秘密由提供者存储,但我无法通过我在控制器中的任何知识方式访问它们。

以下是模型:

user.rb
class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include User::AuthDefinitions
  include User::Roles

  has_many :identities


  field :email, type: String
  field :image, type: String
  field :first_name, type: String
  field :last_name, type: String
  field :roles_mask, type: Integer

  validates_presence_of :email, :first_name, :last_name

  def full_name
    "#{first_name} #{last_name}"
  end

end



identity.rb
class Identity
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :user, index: true

  field :uid, type: String
  field :provider, type: String
  field :token, type: String
  field :secret, type: String
  field :expires_at, type: DateTime

  field :email, type: String
  field :image, type: String
  field :nickname, type: String
  field :first_name, type: String
  field :last_name, type: String

  index({ uid: 1, provider: 1 }, { unique: true })


  def self.from_omniauth(auth)
    identity = where(auth.slice(:provider, :uid)).first_or_create do |identity|
    identity.provider     = auth.provider
    identity.uid          = auth.uid
    identity.token        = auth.credentials.token
    identity.secret       = auth.credentials.secret if auth.credentials.secret
    identity.expires_at   = auth.credentials.expires_at if auth.credentials.expires_at
    identity.email        = auth.info.email if auth.info.email
    identity.image        = auth.info.image if auth.info.image
    identity.nickname     = auth.info.nickname
    identity.first_name   = auth.info.first_name
    identity.last_name    = auth.info.last_name
  end
  identity.save!

  if !identity.persisted?
    redirect_to root_url, alert: "Something went wrong, please try again."
  end
  identity
end

def find_or_create_user(current_user)
  if current_user && self.user == current_user
    # User logged in and the identity is associated with the current user
    return self.user
  elsif current_user && self.user != current_user
    # User logged in and the identity is not associated with the current user
    # so lets associate the identity and update missing info
    self.user = current_user
    self.user.email       ||= self.email
    self.user.image       ||= self.image
    self.user.first_name  ||= self.first_name
    self.user.last_name   ||= self.last_name
    self.user.skip_reconfirmation!
    self.user.save!
    self.save!
    return self.user
  elsif self.user.present?
    # User not logged in and we found the identity associated with user
    # so let's just log them in here
    return self.user
  else
    # No user associated with the identity so we need to create a new one
    self.build_user(
      email: self.email,
      image: self.image,
      first_name: self.first_name,
      last_name: self.last_name,
      roles: [AppConfig.default_role]
    )
    self.user.save!(validate: false)
    self.save!
    return self.user
  end
end

  def create_user

  end

结尾

4

1 回答 1

0

所以查看所有身份应该可以通过

@user = User.find(<<add id here >)
@user.identities.all

要查找特定身份,您可能需要执行以下操作:

@user.identities.where(provider: 'facebook').first

或您为提供者提供的任何字符串。

您还可以为每个身份在用户模型上定义一个方法。

于 2013-10-10T20:08:22.503 回答