Class: DeviseOmniauth::Authentication

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/devise_omniauth/authentication.rb

Class Method Summary collapse

Class Method Details

.find_or_create_for_facebook_oauth(auth_info, signed_in_resource = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/devise_omniauth/authentication.rb', line 6

def self.find_or_create_for_facebook_oauth(auth_info, signed_in_resource=nil)
  # Try to find an existing authentication
  auth = self.where(provider: auth_info.provider, provider_id: auth_info.uid).first
  # No existing authentication was found
  unless auth
    # Try to find a user with the email address contained in auth_info
    # TODO should we have the user confirm they want to link these?
    user = User.find_by_email(auth_info.info.email)
    # If there isn't a user, create one with the auth_info
    # TODO should the user confirm this and/or have the opportunity to link to a different email?
    unless user
      user = User.create(
        name: auth_info.extra.raw_info.name,
        email: auth_info.info.email,
        password: Devise.friendly_token[0, 20]
      )
    end
    # Create the Authentication for the user we found or created
    auth = self.create(
      user: user,
      provider: auth_info.provider,
      provider_id: auth_info.uid
    )
  end
  # Return the Authentication
  auth
end