@@ -7,6 +7,7 @@ export interface EnvVariable {
77 key : string ;
88 value : string | null | undefined ;
99 condition : boolean ;
10+ comment ?: string ;
1011}
1112
1213export async function addEnvVariablesToFile (
@@ -24,7 +25,7 @@ export async function addEnvVariablesToFile(
2425 let contentToAdd = "" ;
2526 const exampleVariables : string [ ] = [ ] ;
2627
27- for ( const { key, value, condition } of variables ) {
28+ for ( const { key, value, condition, comment } of variables ) {
2829 if ( condition ) {
2930 const regex = new RegExp ( `^${ key } =.*$` , "m" ) ;
3031 const valueToWrite = value ?? "" ;
@@ -37,6 +38,9 @@ export async function addEnvVariablesToFile(
3738 modified = true ;
3839 }
3940 } else {
41+ if ( comment ) {
42+ contentToAdd += `# ${ comment } \n` ;
43+ }
4044 contentToAdd += `${ key } =${ valueToWrite } \n` ;
4145 modified = true ;
4246 }
@@ -179,6 +183,22 @@ export async function setupEnvironmentVariables(config: ProjectConfig) {
179183 }
180184 }
181185
186+ if ( backend === "convex" && auth === "better-auth" ) {
187+ if ( hasNextJs ) {
188+ clientVars . push ( {
189+ key : "NEXT_PUBLIC_CONVEX_SITE_URL" ,
190+ value : "https://<YOUR_CONVEX_URL>" ,
191+ condition : true ,
192+ } ) ;
193+ } else if ( hasReactRouter || hasTanStackRouter || hasTanStackStart ) {
194+ clientVars . push ( {
195+ key : "VITE_CONVEX_SITE_URL" ,
196+ value : "https://<YOUR_CONVEX_URL>" ,
197+ condition : true ,
198+ } ) ;
199+ }
200+ }
201+
182202 await addEnvVariablesToFile ( path . join ( clientDir , ".env" ) , clientVars ) ;
183203 }
184204 }
@@ -217,6 +237,43 @@ export async function setupEnvironmentVariables(config: ProjectConfig) {
217237 }
218238
219239 if ( backend === "convex" ) {
240+ if ( auth === "better-auth" ) {
241+ const convexBackendDir = path . join ( projectDir , "packages/backend" ) ;
242+ if ( await fs . pathExists ( convexBackendDir ) ) {
243+ const envLocalPath = path . join ( convexBackendDir , ".env.local" ) ;
244+
245+ if (
246+ ! ( await fs . pathExists ( envLocalPath ) ) ||
247+ ! ( await fs . readFile ( envLocalPath , "utf8" ) ) . includes (
248+ "npx convex env set" ,
249+ )
250+ ) {
251+ const convexCommands = `# Set Convex environment variables
252+ npx convex env set BETTER_AUTH_SECRET=$(openssl rand -base64 32)
253+ npx convex env set SITE_URL http://localhost:3001
254+
255+ ` ;
256+ await fs . appendFile ( envLocalPath , convexCommands ) ;
257+ }
258+
259+ const convexBackendVars : EnvVariable [ ] = [
260+ {
261+ key : hasNextJs
262+ ? "NEXT_PUBLIC_CONVEX_SITE_URL"
263+ : "VITE_CONVEX_SITE_URL" ,
264+ value : "" ,
265+ condition : true ,
266+ comment : "Same as CONVEX_URL but ends in .site" ,
267+ } ,
268+ {
269+ key : "SITE_URL" ,
270+ value : "http://localhost:3001" ,
271+ condition : true ,
272+ } ,
273+ ] ;
274+ await addEnvVariablesToFile ( envLocalPath , convexBackendVars ) ;
275+ }
276+ }
220277 return ;
221278 }
222279
0 commit comments