Sha256: e0c657f35092b56166008102c123f7006ae0533081f0a41a8701ce5858213d95

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module ExvoAuth::Rails::ControllerHelpers
  def self.included(base)
    base.send :include, ExvoAuth::PathHelpers
    base.helper_method :current_user, :signed_in?
  end
  
  def authenticate_user!
    if !signed_in?
      store_location!

      callback_key   = ExvoAuth::Config.callback_key
      callback_value = params[callback_key]

      if request.xhr?
        redirect_to non_interactive_sign_in_path
      elsif callback_value.present?
        redirect_to non_interactive_sign_in_path(callback_key => callback_value)
      else
        redirect_to interactive_sign_in_path
      end
    end
  end
  
  def current_user
    return @current_user if defined?(@current_user)
    @current_user = session[:user_id] && User.find_by_id(session[:user_id])
  end

  def signed_in?
    !!current_user
  end
  
  def store_location!
    session[:return_to] = request.url if request.get?
  end
  
  def stored_location
    session.delete(:return_to)
  end
  
  private
  
  def interactive_sign_in_path(params = {})
    path_with_query("/auth/interactive", params)
  end

  def non_interactive_sign_in_path(params = {})
    path_with_query("/auth/non_interactive", params)
  end
  
  def path_with_query(path, params = {})
    query = Rack::Utils.build_query(params)
    query.empty? ? path : "#{path}?#{query}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
exvo-auth-0.2.2 lib/exvo_auth/rails/controller_helpers.rb