meenie
meenie10mo ago

Preview Deployments and SITE_URL for Convex Auth

I'm setting up Preview Apps for Vercel and I'm not sure how I can set the correct SITE_URL (Required for Convex Auth) because the URL is based on the git branch name, essentially dynamic for each preview app. What's the recommended way to set the SITE_URL to the preview app? Otherwise, I'll need to manually set this every time. Not a big deal, but it would be cool to figure this out :).
22 Replies
Convex Bot
Convex Bot10mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
sshader
sshader10mo ago
Not quite a direct answer here, but I believe you can configure which URL to redirect to from your sign in page (e.g. make it window.location.href), and then provide the redirect callback (https://labs.convex.dev/auth/api_reference/server#callbacksredirect) to allow redirects to any of your preview domains (I'm not sure how much better you can do than "is a subdomain of .vercel.app" or whether you can securely detect that it's one of your preview URLs). The default behavior with no redirect callback and to redirectTo param is to redirect to whatever SITE_URL is set to, and to error if ever trying to redirect to something at a different domain. https://discord.com/channels/1019350475847499849/1305935235183022080 is also related if you're using Convex preview deployments to also preview backend changes, since you'll have a different callback URL for every preview. Looks like there's some tools out there to set up proxying, but IMO it might be easier to add simple email / password auth that's only available in dev / preview
server - Convex Auth
Authentication library for your Convex backend
sam
sam10mo ago
Don’t mean to hijack the thread here, although I trust the OP may be interested in the answer too. Are there any plans for a Vercel integration so that we may have more seamless deployments? If there’s a way to set the SITE_URL in convex for preview to something like NEXT_PUBLIC_WEBSITE_URL=https://$NEXT_PUBLIC_VERCEL_BRANCH_URL we’d be golden. Seems like a chicken and egg thing. I was considering a fixed staging domain on Vercel but diminishes some of the DX advantages.
erquhart
erquhart10mo ago
The Vercel preview deployment instructions in Convex docs have a line that seems relevant: https://docs.convex.dev/production/hosting/vercel#how-it-works-1
It will set the CONVEX_URL (or similarly named) environment variable to point to the new Convex deployment.
I haven't used Convex previews yet myself, but this seems to indicate that Convex is doing something behind the scenes for this exact problem. I may be misunderstanding it, though. Not sure what "or similarly named" means.
sam
sam10mo ago
Thanks! I think the challenge here is the callback url for OAuth uses the CONVEX_URL, then the library uses SITE_URL post authentication to redirect to the front-end. So that’s the value which is perplexing.
erquhart
erquhart10mo ago
Yeah, was hoping the "similarly named" bit maybe implied some inference or configurability, but I guess not
meenie
meenieOP10mo ago
Thank you so much! I'll give the redirect callback a go! Convex is doing something, yes. They will set the appropriate CONVEX_URL. I'm actually not sure how it's detecting it, but it knows I'm using Expo, so it's setting it to PUBLIC_EXPO_CONVEX_URL, which works well :). Convex Auth is separate and is the library requiring SITE_URL to be set. But as @sshader pointed out above, you can override the behavior having to need SITE_URL. I'll just manage it myself based on what env I'm in. I believe it uses the HTTP actions URL, which is the .site TLD as it registers HTTP actions for you. The SITE_URL has to be different because that's where your app lives.
Polderleo
Polderleo3mo ago
Hey! Is this topic already marked as solved? I'm facing the same issue with preview deployments on Netlify. I also saw this mentioned in another thread:
https://discord.com/channels/1019350475847499849/1272605428735283251/1273621306151014442
Would appreciate any updates 🙌
erquhart
erquhart3mo ago
The solution to this issue is Sarah's comment: https://discord.com/channels/1019350475847499849/1309029035812061234/1309174888216395908 Is that not working for you?
Polderleo
Polderleo3mo ago
Okay to DM?
erquhart
erquhart3mo ago
Let's keep it here so it benefits others in the future
Polderleo
Polderleo3mo ago
Jumping in here with a related issue:
In my case, I need VITE_CONVEX_SITE_URL to point to the correct Convex preview (http) URL during Netlify Deploy Previews. Right now, I'm hacking around it by replacing the .cloud URL in my TypeScript, but that's obviously not ideal.
Is there a proper way to pass the preview Convex .SITE URL into the frontend build as an env var? @erquhart do u have any advice on this one? 🙂
erquhart
erquhart3mo ago
For previews I don’t know of a better approach Curious, what’s your use case for needing this in your client at build time?
Polderleo
Polderleo3mo ago
True, so for prod u would hardcode it into the codebase?
erquhart
erquhart3mo ago
Just replacing the way you currently are It could be better if there were known use cases for this, can you share why you need it?
Sean Aguinaga
Sean Aguinaga4w ago
I need it because of this
No description
Sean Aguinaga
Sean Aguinaga4w ago
Also this will never really work with OAuth clients that need to know a redirect URL huh?
Sean Aguinaga
Sean Aguinaga4w ago
Ohhh
No description
Sean Aguinaga
Sean Aguinaga4w ago
A deploy key for a preview deploy Kinda inception-y Never would've known this existed
erquhart
erquhart4w ago
Yeah spas need everything at build time. Not understanding the redirect question, that should work fine as well. For spa the redirect goes through the backend initially but end up at your site URL.
Sean Aguinaga
Sean Aguinaga4w ago
I just tried this - seems fine
No description
Sean Aguinaga
Sean Aguinaga4w ago
With the Preview Deploy key it all works - I can provide the stable URL to Twitter or anyone Don't think there is documentation for this anywhere

Did you find this page helpful?