Skip to content

Instantly share code, notes, and snippets.

@sbonami
Created March 12, 2014 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbonami/9499467 to your computer and use it in GitHub Desktop.
Save sbonami/9499467 to your computer and use it in GitHub Desktop.
Parse/OmniAuth Integration

Required Gems:

  • parse_resource
  • omniauth_facebook
  • koala
class SessionController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end
class User < ParseUser
  delegate :first_name,
           to: :facebook

  class << self
    def from_omniauth(auth)
      User.authenticate_with_facebook(auth[:uid], auth.credentials.token, Time.at(auth.credentials.expires_at)).tap do |user|
        user == false ? self.create_from_omniauth(auth) : user
      end
    end

    def create_from_omniauth(auth)
      User.new.tap do |user|
        user.username = auth.info.nickname
        user.password = auth.uid
        user.save
      end
    end
  end

  def graph
    @_graph ||= Koala::Facebook::API.new(facebook_data.access_token)
  end

  def facebook
    OpenStruct.new(graph.get_object("me"))
  rescue
    false
  end

  private

  def facebook_data
    OpenStruct.new(attributes["authData"]["facebook"])
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment