require './config/environment'
require 'rack-flash'

class ApplicationController < Sinatra::Base

  configure do
    set :public_folder, 'public'
    set :views, 'app/views'
    enable :sessions
    set :session_secret, "you_shall_not_pass"
    use Rack::Flash
  end

  not_found do
    erb :error_404
  end

  get '/' do
    redirect to '/posts' if logged_in?
    
    erb :index
  end

  helpers do

    def current_user
      @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

    def logged_in?
      !!current_user
    end

  end

end