Last active
August 29, 2015 14:02
-
-
Save francois-blanchard/758315317d1b3c257a46 to your computer and use it in GitHub Desktop.
Facebook omniauth rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/environments/develoment.rb | |
ENV['FACEBOOK_APP_ID'] = "XXXXXXXXXXXXX" | |
ENV['FACEBOOK_SECRET'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gemfile | |
gem 'omniauth' | |
gem 'omniauth-facebook' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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