# frozen_string_literal: true

# 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'
  unless logged_in?
    p 'Please log in'
    flash[:danger] = 'Please log in.'
    redirect_to login_url
  end
  @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
end

class SessionsController < ApplicationController
  def new
    render :new
  end

  def create
    user_info = request.env['omniauth.auth']
    session[:user] = user_info.info
    session[:user_id] = user_info.uid
    session[:credentials] = user_info.credentials
    redirect_to root_path, notice: 'Succesful Login'
  end
end
