Sha256: 9eacc43b15649ec9538e155dcf6b6b2b321ef98aacaea567efb435ebe82bdbe4

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 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

            # get an access token from an OAUTH code
            token = Devise::Capturable::API.token(params[:code])
            fail!(:capturable_invalid) unless token['stat'] == 'ok'

            # get the user info form the access token
            entity = Devise::Capturable::API.entity(token['access_token'])
            
            # find user with the capturable params
            user = klass.find_with_capturable_params(entity["result"])

            # if the user exists
            if user
              user.before_capturable_sign_in(entity["result"], params)
            # if the user does not exist
            else
              user = klass.new
              user.before_capturable_create(entity["result"], params)
              user.save!
            end

            # sign in the user
            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.12 lib/devise_capturable/strategy.rb