class SessionController < ApplicationController
  skip_before_action :authenticate_user

  def new
    redirect_to plans_path if current_user
  end

  def create
    user = User.find_by(email: strong_params[:email])
    if user && user.encrypted_password == strong_params[:password]
      session[:user_id] = user.id
      redirect_to plans_path
    else
      flash[:error] = "Bad account/password"
      redirect_to new_session_path
    end
  end

  def logout
    session[:user_id] = nil
    redirect_to '/'
  end

  private

  def strong_params
    params.require(:login).permit(:email, :password)
  end
end
