Sha256: df926ed803078537b05b869333b276ae894706078a8941d4bc1f71549a4264d9

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module Tenzing
  module OauthRegistration
    extend ActiveSupport::Concern

    # TODO: Revisar login desde Google eligiendo otra cuenta ya teniendo una añadida
    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)
        sign_in_and_redirect(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.add_account(@oauth_data)
          flash[:notice] = I18n.t(status, scope: %w(users auth register login), provider: @provider)
          sign_in_and_redirect(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)
          sign_in_and_redirect(auth.user, :event => :authentication)
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tenzing-0.0.1 lib/tenzing/oauth_registration.rb