Supabase limit Sign up to pre-defined email list in a table

100 views Asked by At

I want to limit the registration to a specific pre-defined email list in a table, so I created the following function:

-- limit sign in to the users are pre-defined
CREATE OR REPLACE FUNCTION check_user_email()
RETURNS TRIGGER AS $$
BEGIN
  -- Check if the email is in the allowed_emails table
  IF NEW.email IN (SELECT email FROM allowed_emails) THEN
    RETURN NEW;
  ELSE
    RETURN NULL;
  END IF;
END;
$$ LANGUAGE plpgsql;

and I put the following trigger:

CREATE TRIGGER check_user_email_trigger
BEFORE INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION check_user_email();

but always gives me the following error:

AuthApiError: Database error saving new user
    at handleError (webpack-internal:///(rsc)/./node_modules/@supabase/gotrue-js/dist/main/lib/fetch.js:36:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async _handleRequest (webpack-internal:///(rsc)/./node_modules/@supabase/gotrue-js/dist/main/lib/fetch.js:84:9)
    at async _request (webpack-internal:///(rsc)/./node_modules/@supabase/gotrue-js/dist/main/lib/fetch.js:63:18)
    at async SupabaseAuthClient.signUp (webpack-internal:///(rsc)/./node_modules/@supabase/gotrue-js/dist/main/GoTrueClient.js:218:23)
    at async eval (webpack-internal:///(rsc)/./app/[locale]/page.tsx:67:33) {
  __isAuthError: true,
  status: 500
}

despite that the entered email is found in the allowed_emails table.

I tried to disable the RLS on the allowed_emails table, but it didn't solve the problem.

1

There are 1 answers

0
Emad Naeim On

ok my bad, I forgot to add schema so it will be public.allowed_emails insted of allowed_emails