Files
accounted/supabase/migrations/20260804140000_restore_check_email_exists_rpc.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

38 lines
1.7 KiB
PL/PgSQL

-- =============================================================================
-- Restore public.check_email_exists
-- =============================================================================
--
-- This function shipped in PR #229 and was then lost in the #244 migration
-- consolidation before it ever reached prod: it exists in no deployed
-- environment today. The invite flow still calls it via the service client
-- (app/api/team/accept/route.ts and app/api/company/members/invite/route.ts),
-- so on every deployment the RPC error is silently swallowed, the
-- "already has an account" answer resolves to null, and the invite page
-- routes even existing-account invitees toward /register.
--
-- Restored exactly as originally shipped. SECURITY DEFINER because it reads
-- auth.users; execution is service-role only (the two routes above call it
-- through createServiceClient()) so it cannot be used for email enumeration
-- by anon or authenticated clients.
CREATE OR REPLACE FUNCTION public.check_email_exists(email_to_check text)
RETURNS boolean
LANGUAGE sql
SECURITY DEFINER
SET search_path = ''
AS $$
SELECT EXISTS (
SELECT 1 FROM auth.users WHERE lower(email) = lower(email_to_check)
);
$$;
-- Belt and braces: PUBLIC covers the default grant, but revoke the two
-- browser-facing roles explicitly as well in case a future default-privilege
-- change re-grants them.
REVOKE EXECUTE ON FUNCTION public.check_email_exists(text) FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION public.check_email_exists(text) FROM anon;
REVOKE EXECUTE ON FUNCTION public.check_email_exists(text) FROM authenticated;
GRANT EXECUTE ON FUNCTION public.check_email_exists(text) TO service_role;
NOTIFY pgrst, 'reload schema';