yeah that makes sense, maintaining two versions of everything for guests sounds rough. Thats basically why anon sign in clicked for me, guests just use the same code path as everyone else.
makes sense, and honestly a bit undercounted is fine for that. You mostly care about the trend anyway, not the exact number. One counter per day sounds like the way, good luck with it
thats a clean setup, nothing to clean up or convert at all. The drop off part would bug me tho. Since the preview already goes through your server function you could just count the opens there and compare with signups from invite links. Then you'd at least see how many stop at that step
yeah the local only way sounds like a pain once they register, syncing all that up to the db. Why did you end up dropping guest mode completely, did it not really bring more signups or was it more the extra work?
oh thats actually smart, didn't think of just saving the counts before deleting. Best of both. Gonna set that up when I add the cleanup, thanks
8k in a month is wild lol. Mine are still small numbers so I'm fine for now but good to know how fast it gets out of hand. How did you clean them up, pg_cron or something else?
oh nice, didn't know the id stays the same with updateUser. That makes it way less work than I thought. The existing email case is a good catch, would've 100% run into that captcha is a good shout, gonna add that. For the cleanup I'm leaving them for now on purpose, kinda my lazy traffic stats lol. But yeah once it grows a scheduled job makes sense
good addition, thanks. Yeah same thing with envied that someone mentioned above, it just moves the value from a file into the compiled code. Different place, same result. Search main.dart.js for "sk-" and you'll find it. In the end it's always the same rule: if the client has it, the user has it
Glad it helped! If you get stuck setting it up, feel free to ask here or dm me. The supabase docs on edge functions are pretty good too, there's a section on managing secrets that covers exactly this.
No need to apologize, good question! The key never goes into the app at all. In supabase you set it as a secret on the server, either in the dashboard (edge functions → secrets) or with supabase secrets set OPENAI_API_KEY=sk-.... The edge function reads it at runtime and adds the header itself: const openAiKey = Deno.env.get("OPENAI_API_KEY"); const upstream = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${openAiKey}`, }, body: JSON.stringify({ model, messages, stream: true }), }); the app only sends the user's own session token to the edge function. The function checks that token, then makes the openai call on the server. So the openai key only ever exists on supabase's side. About your setup: the vault + CI part is good, but whatever ends up in the .env still ships inside the apk, like you saw. Whether that's a problem depends on what the key does. If it's a key for your own API and every request still needs a valid user login, the key alone doesn't give anyone access, so that's mostly fine. If it's a key for a third party service (openai, stripe, etc.), someone can just take it and call that service directly. Your login doesn't protect anything there because they never go through your API. Those keys belong on your backend.
Agreed, that's exactly why the openai call moved into the edge function. The only things left in the client are values that are public by design, like the supabase publishable key, and RLS does the actual protecting there.
envied is nice, compile time + no asset file lying around is definitely cleaner than flutter_dotenv. Just worth knowing the obfuscation only slows people down, it doesn't stop them. Fine for public config, but i think real secrets still belong on a server.
Good question, not every key is a secret. The supabase anon / publishable key is meant to be public, it ends up in every client anyway. What actually protects your data is row level security. Without RLS policies anyone with that key can read your tables, so that's the part to get right. The service_role key is the real secret, that one bypasses RLS and should never be in an app. Posthog is similar. The project api key (phc_...) is public by design, it can only send events, not read anything. The personal api key is the one you keep private. Rule of thumb: if a key can only do what a normal user is allowed to do anyway, it can live in the client. If it can do more, it belongs on a server.
Fair point, that was badly worded on my side. Mobile isn't safer, it just looks safer, which is arguably worse because people don't even check. Edited the tldr, thanks for calling it out.
100%, nothing new for anyone who's been around a while. Posted it because flutter_dotenv shows up in a ton of tutorials and beginner repos with real keys in it, and on mobile it at least feels hidden. On web it's one click in devtools. If it saves one person a surprise openai bill it was worth it.
Pretty much. Even with the key on the server I still put the demo on its own openai project with a hard spending limit. If something goes wrong the demo just stops, it doesn't eat into anything else. Feels like the bare minimum once you expose anything AI related publicly.
yeah the dotfile thing cost me way more time than it should have lol. Went with supabase mostly because auth was already there, so checking the user token in the function was basically free. How do you handle rate limiting in your worker? KV or durable objects? I ended up doing it with a postgres function so the check + count happen in one step.
oh good catch, hadn't thought about the race when a msg lands right as the screen renders. Storing created_at + id as a cursor makes way more sense, stealing that lol and yeah moderation is sneaky. If i did blocking on supabase i'd probably put it straight into the rls policy so messages from blocked users just never come back from queries. Then unread counts and the ui are automatically right without extra client logic. Push notifs would still need a check in the edge function before sending tho
Went through basically the same thing building an ai chat app with flutter + supabase. P2P chat and ai chat are 2 totally different problems, so split them up in your head. For unread counts across devices: dont count on the client, thats where it gets messy. I just have a chat_members table with last_read_at per user per chat and unread is just "messages newer than that". Every device reads the same row so it's always in sync. Read receipts kinda come for free from the same column too. Typing / online status i'd keep out of the db completely, supabase realtime presence/broadcast handles that fine. Writing that stuff to tables is how you end up in sync hell. also small tip if you go supabase: in your rls policies use (select auth.uid()) instead of just auth.uid(), otherwise postgres checks it on every row and big message tables get slow af for the ai part +1 to sse. Keep the stream in a StreamSubscription so you can cancel it when the user leaves the screen or hits stop, and only save the final message to the db once it's done, not every chunk. Honestly you dont need a separate websocket backend until you're at real scale, supabase gets you pretty far.
Appreciate that a lot! Glad you found the breakdown helpful.
Spot on! Great callout regarding the buffer cleanup on cancellation/dispose. If `_tokenBuffer.clear()` isn't explicitly called when the user navigates away, those leftover tokens will stay queued and briefly flash if the component gets remounted. Decoupling stream rate from UI frame updates made a night-and-day difference in scrolling performance for us. If anyone wants to see how this buffer logic, stream cancellation, and Supabase RLS integration look in complete production code, feel free to check out our open setup/starter template here: https://dohamasystems.lemonsqueezy.com
100% spot on! That's a super critical distinction. If you're using `StreamSubscription.listen()`, explicitly calling `await subscription.cancel()` in `dispose()` is mandatory to actually close the network socket and stop processing incoming chunks in the background. The `if (!mounted)` check is mainly a safety guard inside async loops or state updates, but cancelling the underlying stream subscription is definitely the real deal to avoid wasted bandwidth and background CPU. Thanks for adding this detail!
That’s a super solid approach for structured outputs! You’re totally right—if you need to parse a complete JSON payload anyway, waiting for the full response makes way more sense than parsing partial chunks. For long-form conversational chat, we went with streaming primarily to cut down perceived latency so users aren't left staring at a loading spinner for seconds. Moving the stream listener/state up to a service/repository layer (like your app-level `ChangeNotifier` approach) is definitely the cleanest way to persist results across screen navigation!