# frozen_string_literal: true

require 'teamsnap'
module SessionsHelper
  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    p 'session is ', session
    !session[:user].nil?
  end

  # Confirms a logged-in user.
  def logged_in_user
    p 'checking login status'
    if logged_in?
      @user = session[:user]
      @user_id = session[:user_id]
      @credentials = session[:credentials]
      TeamSnap.init(token: @credentials['token'],
                    headers: { 'Authorization' => "Bearer #{@credentials['token']}" })
      @teamsnap_client = TeamSnap.root_client
    else
      p 'Please log in'
      flash[:danger] = 'Please log in.'
      redirect_to login_url
    end
  end
end
