Skip to content

Commit

Permalink
Add configurable facebook UID field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowfiend committed May 6, 2009
1 parent 0c4f198 commit 0b25fc2
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions lib/authlogic_facebook_connect/session.rb
Expand Up @@ -6,37 +6,47 @@ def self.included(klass)
include Methods
end
end

module Config
# TODO: Maybe make the facebook_uid field optional
# What user field should be used for the facebook UID?
#
# This is useful if you want to use a single field for multiple types of
# alternate user IDs, e.g. one that handles both OpenID identifiers and
# facebook ids.
#
# * <tt>Default:</tt> :facebook_uid
# * <tt>Accepts:</tt> Symbol
def facebook_uid_field(value = nil)
config(:facebook_uid_field, value, :facebook_uid)
end
alias_method :facebook_uid_field=, :facebook_uid_field
end

module Methods
def self.included(klass)
klass.class_eval do
validate :validate_by_facebook_connect, :if => :authenticating_with_facebook_connect?
end

def credentials=(value)
# TODO: Is there a nicer way to tell Authlogic that we don't have any credentials than this?
values = [:facebook_connect]

super
end
end

def validate_by_facebook_connect
facebook_session = controller.facebook_session

self.attempted_record = klass.find(:first, :conditions => {:facebook_uid => facebook_session.user.uid})


self.attempted_record =
klass.find(:first, :conditions => { facebook_uid_field => facebook_session.user.uid })

unless self.attempted_record
begin
# Get the user from facebook and create a local user
self.attempted_record = klass.new(
:name => facebook_session.user.name,
:facebook_uid => facebook_session.user.uid)

facebook_uid_field => facebook_session.user.uid)

# Save the user without validation as we may have validations for the user that are not met yet
self.attempted_record.save(false)
rescue Facebooker::Session::SessionExpired
Expand All @@ -45,11 +55,10 @@ def validate_by_facebook_connect
end
end
end

def authenticating_with_facebook_connect?
attempted_record.nil? && errors.empty? && controller.facebook_session
end

end
end
end
end

0 comments on commit 0b25fc2

Please sign in to comment.