How to avoid duplicate code in getServerSideProps?

Hey guys, sort of off-topic but lets say I have multiple components with getServerSideProps like this:
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
  const supabase = createServerSupabaseClient(ctx);
  const {
    data: { session },
  } = await supabase.auth.getSession();

  if (!session)
    return {
      notFound: true,
    };

  const { data: customers, error } = await supabase
    .from("customer")
    .select("*");
  if (error) throw new Error(error.message);
  return {
    props: {
      customers,
    },
  };
};

Is there a way I can avoid having to duplicate the portion:
  const supabase = createServerSupabaseClient(ctx);
  const {
    data: { session },
  } = await supabase.auth.getSession();

  if (!session)
    return {
      notFound: true,
    };

every single time I want to check for a session?
Was this page helpful?