mercredi 7 octobre 2015

How to get user object from Facebook rails 4

I need to get the user data's from Facebook for a mobile app. Front End team is using angular Cordova plugin to handle Facebook auth login. They are getting the user info such as name, email, gender, image. My question is how do I save those datas in my database.

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

and here I was stuck, Can anyone please help me out here?

Aucun commentaire:

Enregistrer un commentaire