DEV Community

Cover image for My Invite-Only Gate Lived in the Browser

My Invite-Only Gate Lived in the Browser

MarcoBlch on October 23, 2026

Originally published on meridianbuild.dev, my build log for FastPass, a paid inbox where people pay to reach you and get refunded if you don't repl...
Collapse
 
elijahbrown profile image
Elijah Brown •

The Login tab creating accounts through Google is such an easy one to miss, because with OAuth there's no separate sign up call at all.

On evannn-dev's question about the race: Supabase has a Before User Created auth hook that runs before the row is written to auth.users, and if it returns an error the user is never created. If your invite codes are tied to an email address, the hook can check the incoming email against that list and reject everything else, which would also save you the create-then-delete step for fresh Google accounts. If codes aren't tied to emails it's harder, since the Google signup doesn't carry the code with it, so the check after the callback probably still has to stay.

Either way, putting the hard lock on the Stripe Connect function is the right call. That's the step you can't undo.

Collapse
 
marcoblch profile image
MarcoBlch •

That hook is exactly what I was missing, thanks. Our codes aren't tied to an email, so the Google signup can't carry one, which means the check after the callback stays for now. Tying a code to an email at redemption could let the hook do the job though. Adding it to the list.

Collapse
 
davekurian profile image
Dave Kurian •

One test I’d add to the payout boundary: send a valid JWT for user A with user B’s ID in the body and assert 401, then use an authenticated but uninvited account and assert 403. The unauthenticated check proves the outer gate; these cases pin down that a caller can create a payout account only for themselves and only after passing invite access.

Collapse
 
marcoblch profile image
MarcoBlch •

Good call. The function already compares the JWT user with the userId in the body (401) and checks invite access before Stripe (403), but neither is pinned by a test yet. Writing both.

Collapse
 
davekurian profile image
Dave Kurian •

Good call pinning both cases. A 401 for a mismatched JWT user and a 403 for missing invite access make the identity check and the access policy easy to distinguish. I’d also assert that Stripe is never called in either failure case.

Thread Thread
 
marcoblch profile image
MarcoBlch •

Done, both are pinned now: A's token with B's id gets a 401, an uninvited owner gets a 403. The gate runs before any Stripe call, so neither case ever reaches Stripe.

Thread Thread
 
davekurian profile image
Dave Kurian •

Nice, that closes the two important failure paths: identity mismatch returns 401, and an uninvited owner returns 403 before Stripe is touched. Thanks for pinning the boundary with tests.

Collapse
 
idanshalem profile image
Idan Shalem •

One layer worth checking alongside the edge-function gates is direct database access. supabase-js can query tables through PostgREST without going through your edge functions, so an invite check in a function doesn't cover that route. For exposed tables, RLS policies need to enforce the same access rule. Supabase documents that enabling RLS blocks access through the API with a publishable key until policies allow it. I'd add a direct table query from a non-invited authenticated account to the regression tests, alongside the OAuth and payout cases.

Collapse
 
marcoblch profile image
MarcoBlch •

Fair point, the edge function gate means nothing if PostgREST lets the same account read the table directly. RLS is on for our tables, but I haven't tested them from an uninvited account. Adding that query to the regression set next to the OAuth and payout cases.

Collapse
 
marcoblch profile image
MarcoBlch •

Update: I ran that check. You were right, the database had a side door that the edge function gate couldn't see. Fixed the same day, and I wrote up how I tested it without touching production: meridianbuild.dev/blog/my-invite-g... Thanks for asking the question I was avoiding.

Collapse
 
kamrankhandev profile image
Kamran Khan •

The detail that stuck with me is the two defaults pointing opposite ways. Client catch block set inviteOnlyMode to false, the server version fails closed. I had the same bug shape once: a feature flag fetch failed on the client and opened a gate that should have stayed shut. Flipping the client default to locked was the cheapest fix that ever stuck. Your _shared/invite-gate.ts is the grown-up version of that lesson: one function, one default, and every caller reads from it.

Collapse
 
marcoblch profile image
MarcoBlch •

"Cheapest fix that ever stuck" is the right way to put it. The scary part is that the client default looked harmless in review, it only meant something when the fetch failed.

Collapse
 
evannn-dev profile image
evan •

when the invite_only flag lives only in the DB, what guarantees do you have that the check runs before the user record is persisted, and have you considered a middleware gate to avoid the race?

Collapse
 
marcoblch profile image
MarcoBlch •

Honest answer: nothing guaranteed it at the time, that was the bug. A Google sign in from the Login tab created the auth user before any check ran. Today the check runs right after the OAuth callback and removes uninvited accounts, and the real lock sits on the payout function. Elijah's reply below points to a cleaner fix with the Before User Created hook.