Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exception created by a user inside of a trigger function to bubble up when using signUp/signIn #271

Closed
silentworks opened this issue Nov 8, 2021 · 5 comments · Fixed by #404
Labels

Comments

@silentworks
Copy link

Feature request

Is your feature request related to a problem? Please describe.

It would be good if an exception created by a user inside of a Postgres Trigger function would bubble all the way up to the client code. Currently, I have created an exception but I keep on getting the same default "Database error saving new user" error although I know the signUp process is failing due to my exception. I would prefer to see the message from my exception here.

Another related issue is that the confirmation email is sent before the record gets inserted into the database, not sure why this happens, because by raising an exception the database normally rollback all transactions so the email being sent shouldn't happen before the transaction is completed and successful.

@silentworks silentworks changed the title Allow exception created by users inside of a trigger function to bubble up Allow exception created by a user inside of a trigger function to bubble up Nov 8, 2021
@silentworks silentworks changed the title Allow exception created by a user inside of a trigger function to bubble up Allow exception created by a user inside of a trigger function to bubble up when using signUp/signIn Nov 8, 2021
@kiwicopple
Copy link
Member

Transferring this to our GoTrue server as it will need to be handled there first.

Some code for testing from silentworks:

CREATE OR REPLACE FUNCTION public.username_exists()
    RETURNS TRIGGER 
    LANGUAGE plpgsql 
    SECURITY DEFINER SET search_path = public
    AS
$$
BEGIN
    IF EXISTS(SELECT 1 FROM profiles WHERE username = new.raw_user_meta_data->>'username' LIMIT 1) THEN
        RAISE EXCEPTION 'Username already exists';
    END IF;
    RETURN new;
END
$$;

/* Trigger itself */
CREATE TRIGGER on_auth_user_creation
    BEFORE INSERT ON auth.users
    FOR EACH ROW EXECUTE PROCEDURE public.username_exists();

However, I think we wouldn't want anything leaking from the database so we should only bubble up the error under specific instances.

PostgREST has a good way of handling this - we should use the same so that it's consistent everywhere:

https://postgrest.org/en/v8.0/api.html?highlight=exceptions#raise-errors-with-http-status-codes

eg:

RAISE sqlstate 'PT409' using
  message = 'Username conflict',
  detail = 'Username already exists.',
  hint = 'Try another username';

@kiwicopple kiwicopple transferred this issue from supabase/auth-js Nov 12, 2021
@chipilov
Copy link
Contributor

I think there is a second, related issue here - if a BEFORE trigger modifies the record to be inserted into the table, the API still returns the data that was passed in the request, NOT the data which was actually saved in the auth.users table.

@kiwicopple Do you want a separate issue for that or can it be handled with this one?

@silentworks
Copy link
Author

@chipilov that should be raised as a separate issue as it's not directly related to this one.

@chipilov
Copy link
Contributor

@chipilov that should be raised as a separate issue as it's not directly related to this one.

Ok, opened a separate issue: #288

@github-actions
Copy link
Contributor

github-actions bot commented Jun 7, 2022

🎉 This issue has been resolved in version 2.6.34 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants