如何从Facebook rails获取用户对象4

时间:2015-10-08 04:27:25

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我需要从Facebook获取移动应用的用户数据。前端团队正在使用角度Cordova插件来处理Facebook身份验证登录。他们获取用户信息,如姓名,电子邮件,性别,图像。我的问题是如何在数据库中保存这些数据。

的routes.rb

  resources :users
  post 'user/facebook' => 'users#facebook'

user.rb

class User < ActiveRecord::Base
attr_accessible :email, :name, :uid, :provider, :gender, :oauth_token

def self.fbreg(auth)
    user = User.create(name: auth[:name],
      email: auth[:email],        
      provider: auth[:provider],
      uid: auth[:uid],
      gender: auth[:gender],
      image: auth[:image],
      oauth_token: auth[:oauth_token]
    )
    user.skip_confirmation!
    user
end
end

users_controller.rb

def facebook    
params[:user][:provider] = "facebook"    
@user = User.where("uid = ? OR email = ?",params[:user][:uid],params[:user][:email]).first
if @user.present?      
  sign_in @user
  return render :json => {:success => true, :token => @user.oauth_token, :user => @user}
else
  @reg = User.fbreg(params[:user])      
  sign_in @reg
  return render :json => {:success => true, :token => @reg.oauth_token}
end
end

在这里我被困住了,有人可以帮助我吗?

0 个答案:

没有答案