Module: Devise::Models::FacebookConnectable

Defined in:
lib/devise_facebook_connectable/model.rb

Overview

Facebook Connectable Module, responsible for validating authenticity of a user and storing credentials while signing in using their Facebook account.

Configuration:

You can overwrite configuration values by setting in globally in Devise (Devise.setup), using devise method, or overwriting the respective instance method.

facebook_uid_field - Defines the name of the Facebook user UID database attribute/column.

facebook_session_key_field - Defines the name of the Facebook session key database attribute/column.

facebook_auto_create_account - Speifies if account should automatically be created upon connect

if not already exists.

Examples:

User.facebook_connect(:uid => '123456789')     # returns authenticated user or nil
User.find(1).facebook_connected?               # returns true/false

Defined Under Namespace

Modules: ClassMethods

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

:nodoc:



31
32
33
34
35
# File 'lib/devise_facebook_connectable/model.rb', line 31

def self.included(base) #:nodoc:
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

- (Boolean) facebook_connected? Also known as: is_facebook_connected?

Checks if Facebook Connect:ed.



58
59
60
# File 'lib/devise_facebook_connectable/model.rb', line 58

def facebook_connected?
  self.send(:#{self.class.facebook_uid_field}").present?
end

- (Object) facebook_session(force = false)

Optional: See new_facebook_session.



151
152
153
154
# File 'lib/devise_facebook_connectable/model.rb', line 151

def facebook_session(force = false)
  @facebook_session = self.new_facebook_session if force || @facebook_session.nil?
  @facebook_session
end

- (Object) new_facebook_session

Optional: Recreate Facebook session for this account/user.



139
140
141
142
143
144
145
146
147
# File 'lib/devise_facebook_connectable/model.rb', line 139

def new_facebook_session
  timeout_in = ::Devise.respond_to?(:timeout_in) ? ::Devise.timeout_in : 1.hour.from_now
  returning(::Facebooker::Session.create) do |new_session|
    new_session.secure_with!(self.send(:#{self.class.facebook_session_key_field}"),
        self.send(:#{self.class.facebook_uid_field}"), timeout_in
      )
    ::Facebooker::Session.current = new_session
  end
end

- (Object) on_after_facebook_connect(facebook_session)

Hook that gets called after connect (each time). Useful for fetching additional user info (etc.) from Facebook.

Default: Do nothing.

Example:

# Overridden in Facebook Connect:able model, e.g. "User".
#
def after_facebook_connect(fb_session)
  # See "on_before_facebook_connect" example.
end


123
124
125
126
127
# File 'lib/devise_facebook_connectable/model.rb', line 123

def on_after_facebook_connect(facebook_session)
  if self.respond_to?(:after_facebook_connect)
    self.send(:after_facebook_connect, facebook_session) rescue nil
  end
end

- (Object) on_before_facebook_connect(facebook_session)

Hook that gets called before connect (each time). Useful for fetching additional user info (etc.) from Facebook.

Default: Do nothing.

Examples:

# Overridden in Facebook Connect:able model, e.g. "User".
#
def before_facebook_connect(fb_session)

  # Just fetch what we really need from Facebook...
  fb_session.user.populate(:locale, :current_location, :username, :name,
                            :first_name, :last_name, :birthday_date, :sex,
                            :city, :state, :country)

  self.locale             = my_fancy_locale_parser(fb_session.user.locale)
  "Stockholm" => "(GMT+01:00) Stockholm", "Never-Never-land" => "(GMT+00:00) UTC"
  self.time_zone          = fb_session.user.current_location.try(:city)
  self.country            = fb_session.user.current_location.try(:country)

  self.username           = fb_session.user.username

  self.profile.real_name  = fb_session.user.name
  self.profile.first_name = fb_session.user.first_name
  self.profile.last_name  = fb_session.user.last_name
  self.profile.birthdate  = fb_session.user.birthday_date.try(:to_date)
  self.profile.gender     = my_fancy_gender_parser(fb_session.user.sex)

  self.profile.city       = fb_session.user.hometown_location.try(:city)
  self.profile.zip_code   = fb_session.user.hometown_location.try(:state)
  self.profile.country    = fb_session.user.hometown_location.try(:country)

  # etc...

end

For more info:

* http://facebooker.pjkh.com/user/populate


104
105
106
107
108
# File 'lib/devise_facebook_connectable/model.rb', line 104

def on_before_facebook_connect(facebook_session)
  if self.respond_to?(:before_facebook_connect)
    self.send(:before_facebook_connect, facebook_session) rescue nil
  end
end

- (Object) store_facebook_credentials!(attributes = {})

Store Facebook Connect account/session credentials.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/devise_facebook_connectable/model.rb', line 39

def store_facebook_credentials!(attributes = {})
  self.send(:#{self.class.facebook_uid_field}=", attributes[:uid])
  self.send(:#{self.class.facebook_session_key_field}=", attributes[:session_key])

  # Confirm without e-mail - if confirmable module is loaded.
  self.skip_confirmation! if self.respond_to?(:skip_confirmation!)

  # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used).
  self.email = attributes[:email] || '' if self.respond_to?(:email)

  # Lazy hack: These database fields are required if +authenticable+/+confirmable+
  # module(s) is used. Could be avoided with :null => true for authenticatable
  # migration, but keeping this to avoid unnecessary problems.
  self.password_salt = '' if self.respond_to?(:password_salt)
  self.encrypted_password = '' if self.respond_to?(:encrypted_password)
end

- (Object) store_session(using_session_key)

Optional: Store session key.



131
132
133
134
135
# File 'lib/devise_facebook_connectable/model.rb', line 131

def store_session(using_session_key)
  if self.session_key != using_session_key
    self.update_attribute(self.send(:#{self.class.facebook_session_key_field}"), using_session_key)
  end
end