Module: Omniauth::MultipleProviders::Omniauthable::ClassMethods

Defined in:
lib/omniauth/multiple_providers/models/concerns/omniauthable.rb

Instance Method Summary collapse

Instance Method Details

#create_by_oauth(auth) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omniauth/multiple_providers/models/concerns/omniauthable.rb', line 62

def create_by_oauth(auth)
  case auth['provider']
  when 'twitter'
    # Twitter not give me email from api
    User.create_by_twitter(auth)
  when 'facebook', 'google_oauth2', 'github'
    # Other api give me email
    User.create_by_omniauth(auth)
  else
    User.new
  end
end

#create_by_omniauth(auth) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/omniauth/multiple_providers/models/concerns/omniauthable.rb', line 75

def create_by_omniauth(auth)
  u = User.new
  u.email = auth['info']['email']
  u.name = auth['info']['name']
  password = Devise.friendly_token[0,20]
  u.password = password
  u.password_confirmation = password
  u.skip_confirmation! # Because we get confirmed (by providers) email !
  u.save
  u
end

#create_by_twitter(auth) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/omniauth/multiple_providers/models/concerns/omniauthable.rb', line 87

def create_by_twitter(auth)
  # https://github.com/arunagw/omniauth-twitter#authentication-hash
  u = User.new
  # u.email = auth['info']['email'] # Twitter not give me email from api
  u.name = auth['info']['name']
  password = Devise.friendly_token[0,20]
  u.password = password
  u.password_confirmation = password
  u.save
  u
end

#find_by_oauth(auth) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omniauth/multiple_providers/models/concerns/omniauthable.rb', line 47

def find_by_oauth(auth)
  if up = ProviderUser.find_by(uid: auth['uid'], provider: auth['provider'])
    up.user
  else
    # Hmmm...
    # FIXME to be configuratable
    # Do this If you trust provider email
    if user = User.find_by(email: auth['email'])
      user
    else
      nil
    end
  end
end

#find_or_create_by_oauth(auth, current_user) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omniauth/multiple_providers/models/concerns/omniauthable.rb', line 30

def find_or_create_by_oauth(auth, current_user)
  @user = if current_user
    current_user.create_or_update_provider_user_by(auth)
    current_user
  else
    if user = User.find_by_oauth(auth)
      user.create_or_update_provider_user_by(auth)
      user
    else
      user = User.create_by_oauth(auth)
      user.save
      user.create_or_update_provider_user_by(auth)
      user
    end
  end
end