Sha256: 433242c2d17e3a4146374a509ffb723398ca3a91f8e5d23fce756f3a9cb58cc3

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'devise_capturable/api'

module Devise
  
  module Capturable
    
    module Strategies

      class Capturable < ::Devise::Strategies::Base

        def valid?
          valid_controller? && valid_params? && mapping.to.respond_to?(:find_with_capturable_params) && mapping.to.method_defined?(:before_capturable_create) && mapping.to.method_defined?(:before_capturable_sign_in)
        end

        def authenticate!
          
          klass = mapping.to

          begin
            token = Devise::Capturable::API.token(params[:code])
            fail!(:capturable_invalid) unless token['stat'] == 'ok'
              
            entity = Devise::Capturable::API.entity(token['access_token'])
            user = klass.find_with_capturable_params(entity["result"])

            if user
              user.before_capturable_sign_in(entity["result"], params)
            else
              user = klass.new
              user.before_capturable_create(entity["result"], params)
              user.save!
            end

            success!(user)
          rescue Exception => e
            fail!("Login failed: #{e.to_s}")
          end
        end
        
        protected
          
        def valid_controller?
          params[:controller].to_s =~ /sessions/
        end

        def valid_params?
          params[:code].present?
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_capturable-0.0.9 lib/devise_capturable/strategy.rb