Class: Pwb::User

Inherits:
ApplicationRecord show all
Defined in:
app/models/pwb/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_for_oauth(auth) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/pwb/user.rb', line 16

def self.find_for_oauth(auth)
  authorization = Authorization.where(provider: auth.provider, uid: auth.uid.to_s).first
  return authorization.user if authorization

  email = auth.info[:email]
  unless email.present?
    # below is a workaround for when email is not available from auth provider
    email = "#{SecureRandom.urlsafe_base64}@example.com"
    # in future might redirect to a page where email can be requested
  end
  user = User.where(email: email).first
  if user
    user.create_authorization(auth)
  else
    # need to prefix Devise with :: to avoid confusion with Pwb::Devise
    password = ::Devise.friendly_token[0, 20]
    user = User.create!(email: email, password: password, password_confirmation: password)
    user.create_authorization(auth)
  end

  user
end

Instance Method Details

#create_authorization(auth) ⇒ Object



39
40
41
# File 'app/models/pwb/user.rb', line 39

def create_authorization(auth)
  authorizations.create(provider: auth.provider, uid: auth.uid)
end

#default_client_localeObject

TODO: - use db col for below



12
13
14
# File 'app/models/pwb/user.rb', line 12

def default_client_locale
  :en
end