class SessionsController < ApplicationController
# before_save :send_email  #this can be used to handle sign ups via fb
    def new
      @errors = []
    end

    def create
        @reader = Reader.find_by(email: params[:reader][:email])
        if @reader && @reader.authenticate(params[:reader][:password])
            session[:reader_id] = @reader.id
            redirect_to "/home"
        elsif @reader
            @errors = ["Invalid Password"]
            render :new
          else
            @errors = ["Invalid Username"]
            render :new
          end
    end

    def create_with_fb
        
        reader = Reader.find_or_create_by(email: fb_auth['info']['email'], username: fb_auth['info']['name']) do |r|
          r.password = 'password' #we set their password here and it doesn't matter what it is, could autogenerate it
          #here we can send them an email with their temporary/current autogenerated pw which they can change at a later time
        end
        if reader.save
          session[:reader_id] = reader.id
          redirect_to "/home"
        else
          redirect_to signup_path
        end
    end
    
    def destroy
        session.clear
        redirect_to '/login'
    end

    private

        def fb_auth
            self.request.env['omniauth.auth']
        end
        
    
end