Module: AuthlogicFacebookConnect::Session::Methods

Defined in:
lib/authlogic_facebook_connect/session.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/authlogic_facebook_connect/session.rb', line 64

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

Instance Method Details

#authenticating_with_facebook_connect?Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/authlogic_facebook_connect/session.rb', line 113

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

#credentials=(value) ⇒ Object



69
70
71
72
73
# File 'lib/authlogic_facebook_connect/session.rb', line 69

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

#validate_by_facebook_connectObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/authlogic_facebook_connect/session.rb', line 76

def validate_by_facebook_connect
  facebook_session = controller.facebook_session
  self.attempted_record = facebook_user_class.find(:first, :conditions => { facebook_uid_field => facebook_session.user.uid }).try(:"#{klass}".to_s.underscore)

  unless self.attempted_record || facebook_skip_new_user_creation
    begin
      # Get the user from facebook and create a local user.
      #
      # We assign it after the call to new in case the attribute is protected.
      new_user = klass.new
      if klass == facebook_user_class
        new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
      else
        new_user.send(:"build_#{facebook_user_class.to_s.underscore}", :"#{facebook_uid_field}" => facebook_session.user.uid)
      end

      new_user.before_connect(facebook_session) if new_user.respond_to?(:before_connect)
      
      self.attempted_record = new_user
      
      if facebook_valid_user
        errors.add_to_base(
          I18n.t('error_messages.facebook_user_creation_failed',
                 :default => 'There was a problem creating a new user ' +
                             'for your Facebook account')) unless self.attempted_record.valid?

        self.attempted_record = nil
      else
        self.attempted_record.save_with_validation(false)
      end
    rescue Facebooker::Session::SessionExpired
      errors.add_to_base(I18n.t('error_messages.facebooker_session_expired', 
        :default => "Your Facebook Connect session has expired, please reconnect."))
    end
  end
end