Skip to content

Instantly share code, notes, and snippets.

@francois-blanchard
Last active August 29, 2015 14:02
Show Gist options
  • Save francois-blanchard/758315317d1b3c257a46 to your computer and use it in GitHub Desktop.
Save francois-blanchard/758315317d1b3c257a46 to your computer and use it in GitHub Desktop.
Facebook omniauth rails
# app/views/layout/application.html.rb
<div class="provider">
<% unless current_user.link_provider?('facebook') %>
<%= link_to "Link your facebook profile", "/auth/facebook" %>
<% else %>
<%= link_to "Delete facebook profile linking", "/delete/facebook/profile" %>
<% end %>
</div>
# config/environments/develoment.rb
ENV['FACEBOOK_APP_ID'] = "XXXXXXXXXXXXX"
ENV['FACEBOOK_SECRET'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# Gemfile
gem 'omniauth'
gem 'omniauth-facebook'
# app/controllers/identiies_controller.rb
# encoding: utf-8
class IdentitiesController < ApplicationController
def create
current_user.find_for_oauth(env["omniauth.auth"])
redirect_to("/#{current_user.role}/home", :flash => {:success => "Merci #{current_user.facebook.name}, votre profile facebook est correctement lié à notre plateforme"})
end
def destroy
if current_user.identity_destroy(params[:provider])
redirect_to("/#{current_user.role}/home", :flash => {:success => "Votre profile #{params[:provider]} a bien été dissocié de notre plateforme"})
else
redirect_to("/#{current_user.role}/home", :flash => {:error => "Sorry there was an error"})
end
end
end
# app/models/identity.rb
class Identity < ActiveRecord::Base
attr_accessible :provider, :uid, :name, :oauth_token, :oauth_expires_at, :user_id
belongs_to :user
validates_presence_of :uid, :provider
def self.find_for_oauth(auth, user_id)
identity = Identity.where(:provider => auth.provider, :uid => auth.uid).first
identity = Identity.create(:provider => auth.provider, :uid => auth.uid, :name => auth.info.name, :oauth_token => auth.credentials.token, :oauth_expires_at => Time.at(auth.credentials.expires_at), :user_id => user_id) if identity.nil?
identity
end
end
# config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'], {:scope => "public_profile, user_friends, email, user_about_me, user_activities, user_birthday, user_events, user_interests, user_likes, user_location, user_status, user_tagged_places, user_website, read_friendlists, read_insights, read_stream, manage_notifications"}
end
# config/routes.rb
###########################
# Provider connect routes #
###########################
match 'auth/:provider/callback', to: 'identities#create'
match 'auth/failure', to: redirect('/')
match 'delete/:provider/profile', to: 'identities#destroy'
# app/models/user.rb
def find_for_oauth(auth)
identity = Identity.find_for_oauth(auth, self.id)
end
def link_provider?(provider)
Identity.where(:provider => provider, :user_id => self.id).present?
end
def identity_destroy(provider)
identity = self.identities.where(:provider => provider).first
if identity.present?
identity.delete
true
else
false
end
end
def facebook
self.identities.where(:provider => 'facebook').first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment