import { redirect } from "next/navigation";
import { supabaseServer } from "@/lib/supabase/server";

export const requireUser = async (redirectTo: string) => {
  const supabase = await supabaseServer();
  if (!supabase) {
    return null;
  }

  const { data } = await supabase.auth.getUser();
  if (!data.user) {
    redirect(`/auth/login?redirectTo=${encodeURIComponent(redirectTo)}`);
  }

  return data.user;
};
