-
-
Save rudzainy/d17155fda03631a2ddd5 to your computer and use it in GitHub Desktop.
Add facebook auth to a Clearance app, using 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
class Authentication < ActiveRecord::Base | |
belongs_to :user | |
def self.create_with_omniauth(auth_hash) | |
create! do |auth| | |
auth.provider = auth_hash["provider"] | |
auth.uid = auth_hash["uid"] | |
auth.token = auth_hash["credentials"]["token"] | |
end | |
end | |
def update_token(auth_hash) | |
self.token = auth_hash["credentials"]["token"] | |
self.save | |
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
gem 'omniauth' | |
gem 'omniauth-facebook' | |
gem 'clearance' |
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
Rails.application.config.middleware.use OmniAuth::Builder do | |
provider :facebook, 'APP_KEY', 'APP_SECRET' | |
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
http://stackoverflow.com/questions/7668093/facebook-auth-for-a-rails-app-using-clearance | |
https://richonrails.com/articles/facebook-authentication-in-ruby-on-rails | |
http://railscasts.com/episodes/360-facebook-authentication?view=asciicast |
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
match "/auth/:provider/callback" => "sessions#create_from_omniauth" |
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
class SessionsController < Clearance::SessionsController | |
def create_from_omniauth | |
auth_hash = request.env["omniauth.auth"] | |
authentication = Authentication.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) || Authentication.create_with_omniauth(auth_hash) | |
if authentication.user | |
user = authentication.user | |
authentication.update_token(auth_hash) | |
@next = root_url | |
@notice = "Signed in!" | |
else | |
user = User.create_with_auth_and_hash(authentication,auth_hash) | |
@next = edit_user_path(user) | |
@notice = "User created - confirm or edit details..." | |
end | |
sign_in(user) | |
redirect_to @next, :notice => @notice | |
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
class User < ActiveRecord::Base | |
include Clearance::User | |
has_many :authentications, :dependent => :destroy | |
def self.create_with_auth_and_hash(authentication,auth_hash) | |
create! do |u| | |
u.first_name = auth_hash["info"]["first_name"] | |
u.last_name = auth_hash["info"]["last_name"] | |
u.friendly_name = auth_hash["info"]["name"] | |
u.email = auth_hash["extra"]["raw_info"]["email"] | |
u.authentications<<(authentication) | |
end | |
end | |
def fb_token | |
x = self.authentications.where(:provider => :facebook).first | |
return x.token unless x.nil? | |
end | |
def password_optional? | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment