Use postgres function for username eligibility check?

Hi all, I am storing users’ usernames in a profiles table separate from auth and want to check on signup whether a username is unique or not. I thought I could do this via database functions instead of edge functions. But I’m unsure how to set it up w/ Weweb workflows at the moment.

  1. does the function naming matter? does it have to match up exactly w/ the supabase postgres function name?
  2. within the custom JS section, I have the following code. What do I specify as the client value? Or is the whole thing wrong?
async function checkUsernameAvailability(supabaseClient, username) {
    const { data, error } = await supabaseClient
        .rpc('check_username_availability', { uname: username });

    if (error) {
        console.error('Error checking username availability:', error);
        return;
    }

    if (data) {
        console.log('Username is taken');
    } else {
        console.log('Username is available');
    }
}

Thank you so much!

the function name must be the same when you are inputting the “Function name option” for the rest, it’s not required.

I don’t quite understand what’s going on in your arguments. If your postgres function is called checkUsernameAvailability then you just set it as Function name (you already did that) and for arguments, you just do username and bind [username variable]. No need to call all that JS, you’re basically already doing that by using the workflow action, the workflow action is the equivalent of that JS you have there.

I’m gonna assume your function is called check_username_availability, then this is the setup you need to do :slight_smile:

As for the console.logs you just handle those in your next no-code workflow action.

1 Like

Hey, I’ve just spent a few days finding a workaround to not use email addresses but usernames instead. So this might be useful.

One I have multi tenancy setup vaguely following this guide Supabase Multi Tenancy - Simple and Fast - by Ryan O'Neill

Then I have created an edge function to create users without being kicked out of the app as is default following this (whch will work as is copy pasted if you want)

So far this is my workflow

and this is the username check

my username format is firstname(+n)@tenant_name

Hopefully this gives some inspiration