Module: Tenzing::OauthRegistration

Extended by:
ActiveSupport::Concern
Defined in:
lib/tenzing/oauth_registration.rb

Instance Method Summary collapse

Instance Method Details

#register_user(data) ⇒ Object

TODO: Revisar login desde Google eligiendo otra cuenta ya teniendo una añadida



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
33
34
35
36
37
38
39
40
41
42
# File 'lib/tenzing/oauth_registration.rb', line 6

def register_user(data)
  auth = Authentication.where(
      uid: data[:auth][:uid], provider: data[:auth][:provider]
  ).first_or_initialize

  if auth.user.present?
    # Sign in the authentication user
    flash[:notice] = I18n.t('logged', scope: %w(users auth register), provider: @provider)
    (auth.user, :event => :authentication)
  else
    # NO EXISTE AUTHENTICATION -> COMPROBAR EMAIL
    if user = User.find_by_email(data[:user][:email])
      # SI EXISTE EMAIL -> SE AÑADE AUTHENTICATION
      status = user.(@oauth_data)
      flash[:notice] = I18n.t(status, scope: %w(users auth register login), provider: @provider)
      (user, :event => :authentication)
    else
      # NO EXISTE EMAIL -> SE REGISTRA
      auth.auth_token = data[:auth][:auth_token]
      auth.auth_secret = data[:auth][:auth_token]
      auth.build_user(
          email: data[:user][:email],
          name: data[:user][:name],
          password: (temp_pass = SecureRandom.hex(10)),
          password_confirmation: temp_pass,
          terms: '1', # Acceptance terms
          # TODO: Fix SSL problem in development
          avatar: (open(data[:user][:image], allow_redirections: :safe) rescue nil)
      )
      auth.user.skip_confirmation!
      auth.user.save!
      auth.save!
      flash[:notice] = I18n.t('registered', scope: %w(users auth register), provider: @provider)
      (auth.user, :event => :authentication)
    end
  end
end