Module: Empower::OmniAuth::ClassMethods

Defined in:
lib/empower/omniauth.rb

Instance Method Summary collapse

Instance Method Details

#from_omniauth(auth) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/empower/omniauth.rb', line 10

def from_omniauth(auth)
  user = User.find_by_email(auth.info.email)
  if user.nil?
    user = User.create!(
      :email => auth.info.email,
      :password => Devise.friendly_token[0,20],
    )
    attrs = {}
    if ActiveRecord::Base.connection.column_exists?(:users, :name, :string)
      attrs[:name] = auth.info.name
    end
    if ActiveRecord::Base.connection.column_exists?(:users, :image, :string)
      attrs[:image] = auth.info.image
    end
    if attrs.keys.size > 0
      user.update_columns(attrs)
    end
  else
    attrs = {}
    if ActiveRecord::Base.connection.column_exists?(:users, :name, :string)
      attrs[:name] = auth.info.name
    end
    if ActiveRecord::Base.connection.column_exists?(:users, :image, :string)
      attrs[:image] = auth.info.image
    end
    if attrs.keys.size > 0
      user.update_columns(attrs)
    end
  end
  user
end

#new_with_session(params, session) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/empower/omniauth.rb', line 42

def new_with_session(params, session)
  super.tap do |user|
    if data = session["devise.facebook_data"] &&
      session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
    end
  end
end