I have a function which checks if a user with an email is allowed to signup. However, this function works correctly when I run it manually with a valid email, but not when I use
supabase.auth.signUp
supabase.auth.signUp
in my sveltekit app. Manually, it returns
{}
{}
but when a user really tries to sign up, it returns
Signups from this email (<redacted>) are not allowed.
Signups from this email (<redacted>) are not allowed.
for the same email.
Any idea why?
The function in question:
DECLARE user_email text; allowed int;BEGIN user_email := (event -> 'user' ->> 'email'); -- if no email provided, deny IF user_email IS NULL THEN RETURN jsonb_build_object( 'error', jsonb_build_object( 'message', 'No email provided in signup event.', 'http_code', 400 ) ); END IF; SELECT COUNT(*) INTO allowed FROM public.allowed_users au WHERE lower(au.email) = lower(user_email); IF allowed > 0 THEN -- empty object indicates success for Supabase Auth hook RETURN '{}'::jsonb; END IF; RETURN jsonb_build_object( 'error', jsonb_build_object( 'message', format('Signups from this email (%s) are not allowed.', user_email), 'http_code', 403 ) );END;
DECLARE user_email text; allowed int;BEGIN user_email := (event -> 'user' ->> 'email'); -- if no email provided, deny IF user_email IS NULL THEN RETURN jsonb_build_object( 'error', jsonb_build_object( 'message', 'No email provided in signup event.', 'http_code', 400 ) ); END IF; SELECT COUNT(*) INTO allowed FROM public.allowed_users au WHERE lower(au.email) = lower(user_email); IF allowed > 0 THEN -- empty object indicates success for Supabase Auth hook RETURN '{}'::jsonb; END IF; RETURN jsonb_build_object( 'error', jsonb_build_object( 'message', format('Signups from this email (%s) are not allowed.', user_email), 'http_code', 403 ) );END;
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.