module JumpIn::Authentication

Public Class Methods

included(base) click to toggle source
# File lib/jump_in/authentication.rb, line 6
def self.included(base)
  base.extend(ClassMethods)
  base.send :helper_method, :current_user, :logged_in? if
    base.respond_to? :helper_method
  base.const_set('GET_CURRENT_USER', [])
  const_set('APP_MAIN_CONTROLLER', base)
end

Public Instance Methods

authenticate_by_strategy(user:, auth_params:) click to toggle source
# File lib/jump_in/authentication.rb, line 24
def authenticate_by_strategy(user:, auth_params:)
  if strategy = detected_strategy(user: user, auth_params: auth_params)
    strategy.authenticate_user
  else
    false
  end
end
current_user() click to toggle source

HELPER METHODS

# File lib/jump_in/authentication.rb, line 48
def current_user
  return @current_user if defined?(@current_user)
  @current_user = get_current_user
end
jump_in(user:, **auth_params) click to toggle source

LOGGING IN

# File lib/jump_in/authentication.rb, line 15
def jump_in(user:, **auth_params)
  if !logged_in? && authenticate_by_strategy(user: user,
                                             auth_params: auth_params)
    login(user: user)
  else
    return false
  end
end
jump_out() click to toggle source

LOGGING OUT

# File lib/jump_in/authentication.rb, line 41
def jump_out
  self.class::ON_LOGOUT.each { |on_logout| send(on_logout) }
  true
end
logged_in?() click to toggle source
# File lib/jump_in/authentication.rb, line 53
def logged_in?
  !!current_user
end
login(user:) click to toggle source
# File lib/jump_in/authentication.rb, line 32
def login(user:)
  self.class::ON_LOGIN.each do |on_login|
    send(on_login, user: user)
  end
  true
end

Private Instance Methods

detected_strategy(user:, auth_params:) click to toggle source
# File lib/jump_in/authentication.rb, line 89
def detected_strategy(user:, auth_params:)
  if the_strategy = JumpIn::Strategies::Base::STRATEGIES
                    .detect { |strategy| strategy.detected?(auth_params) }
    the_strategy.new(user: user, auth_params: auth_params)
  else
    fail JumpIn::AuthenticationStrategyError
  end
end
get_current_user() click to toggle source

PRIVATE

# File lib/jump_in/authentication.rb, line 80
def get_current_user
  current_user = nil
  self.class.const_get(:GET_CURRENT_USER).each do |current_user_finder|
    current_user = send(current_user_finder)
    break if current_user
  end
  current_user
end