Skip to content

Instantly share code, notes, and snippets.

@ilyabrin
Created November 7, 2011 09:47
Show Gist options
  • Save ilyabrin/1344568 to your computer and use it in GitHub Desktop.
Save ilyabrin/1344568 to your computer and use it in GitHub Desktop.
Twitter, Facebook Login in Rails
def facebook_log_in
# Scenarios
# If the user is signed in and has no facebook uid we link the accounts
# If the user is not signed in we create an account for him.
# that's it. he is logged in for life.
if player_signed_in?
if current_player['fb_uid'].nil?
# Link accounts
puts "linking accounts"
graph = Koala::Facebook::API.new(@fb_user['access_token'])
player_details = graph.get_object("me")
Player.find(current_player['_id']).update_attributes({
:fb_name => player_details['name'],
:fb_uid => @fb_user['user_id'],
:fb_token => @fb_user['access_token']
})
else
# Do nothing. the user is logged in and he has facebook credentials.
# This means a user with facebook connection will be logged in forever.
# No matter what. muhahaha.
end
else
# Parse Facebook cookies
@fb_oauth = Koala::Facebook::OAuth.new('278614762170622', '941951e5795a34dd9d252cbb20569438')
@fb_user = @fb_oauth.get_user_info_from_cookies(cookies)
unless @fb_user.nil?
# See if we already have the user in the system
# this will never be called, since we get an access
# token for life, so once the user is created
# he is logged in forever.
# Player.first(conditions: {fb_uid: @fb_user['user_id']}
# else:
# We create a new player
puts "creating a new user"
graph = Koala::Facebook::API.new(@fb_user['access_token'])
player_details = graph.get_object("me")
@player = Player.create!(
:fb_name => player_details['name'],
:fb_uid => @fb_user['user_id'],
:fb_token => @fb_user['access_token']
)
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @player, :event => :authentication
end
end
end
def twitter
# Scenarios:
# 1. Player is already signed in with his fb account:
# we link the accounts and update the information.
# 2. Player is new: we create the account.
# 3. Player is old: we update the player's information.
# login with a safe write.
twitter_details = {
twitter_name: env["omniauth.auth"]['user_info']['name'], # Almog Melamed
twitter_nick: env["omniauth.auth"]['user_info']['nickname'], # @radagaisus
twitter_uid: env["omniauth.auth"]['uid'] # Twitter uid
}
if player_signed_in?
@player = Player.find(current_player['_id'])
else
@player = Player.first(conditions: {twitter_uid: env['omniauth.auth']['uid']})
end
if @player.nil?
@player = Player.create!(twitter_details)
else
@player.update_attributes(twitter_details)
end
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Twitter"
sign_in_and_redirect @player, :event => :authentication
end
def failure
puts "signing in using omniauth failed."
flash[:notice] = I18n.t "devise.omniauth_callbacks.failure", :kind => "Twitter"
redirect_to :root
end
# Old Method using Server Side for Facebook
def facebook
@player = Player.where(fb_uid: env['omniauth.auth']['uid']).first
if @player.nil?
@player = Player.create! :fb_name => env["omniauth.auth"]['user_info']['name'],
:fb_uid => env["omniauth.auth"]['uid'],
:fb_token => env['omniauth.auth']['credentials']['token']
end
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @player, :event => :authentication
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment