Sha256: 711d80b3e4888cfe399d0984c3d1c06e7608c1ac84652518a1231370d0bd5f39

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 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
            puts e.inspect
            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

2 entries across 2 versions & 1 rubygems

Version Path
devise_capturable-0.0.11 lib/devise_capturable/strategy.rb
devise_capturable-0.0.10 lib/devise_capturable/strategy.rb