@@ -5,7 +5,7 @@ import pc from "picocolors";
55import { PKG_ROOT } from "../constants" ;
66import type { ProjectConfig } from "../types" ;
77
8- export async function configureAuth (
8+ export async function setupAuth (
99 projectDir : string ,
1010 enableAuth : boolean ,
1111 hasDatabase : boolean ,
@@ -16,177 +16,98 @@ export async function configureAuth(
1616
1717 try {
1818 if ( ! enableAuth ) {
19- await fs . remove ( path . join ( clientDir , "src/components/sign-up-form.tsx" ) ) ;
20- await fs . remove ( path . join ( clientDir , "src/components/sign-in-form.tsx" ) ) ;
21- await fs . remove ( path . join ( clientDir , "src/components/auth-forms.tsx" ) ) ;
22- await fs . remove ( path . join ( clientDir , "src/components/user-menu.tsx" ) ) ;
23- await fs . remove ( path . join ( clientDir , "src/lib/auth-client.ts" ) ) ;
24-
25- const indexRoutePath = path . join ( clientDir , "src/routes/index.tsx" ) ;
26- const indexRouteContent = await fs . readFile ( indexRoutePath , "utf8" ) ;
27- const updatedIndexRouteContent = indexRouteContent
28- . replace ( / i m p o r t A u t h F o r m s f r o m " @ \/ c o m p o n e n t s \/ a u t h - f o r m s " ; \n / , "" )
29- . replace ( / < A u t h F o r m s \/ > / , "" ) ;
30- await fs . writeFile ( indexRoutePath , updatedIndexRouteContent , "utf8" ) ;
31-
32- await fs . remove ( path . join ( serverDir , "src/lib/auth.ts" ) ) ;
33-
34- const indexFilePath = path . join ( serverDir , "src/index.ts" ) ;
35- const indexContent = await fs . readFile ( indexFilePath , "utf8" ) ;
36- const updatedIndexContent = indexContent
37- . replace ( / i m p o r t { a u t h } f r o m " \. \/ l i b \/ a u t h " ; \n / , "" )
38- . replace (
39- / a p p \. o n \( \[ " P O S T " , " G E T " \] , " \/ a p i \/ a u t h \/ \* \* " , \( c \) = > a u t h \. h a n d l e r \( c \. r e q \. r a w \) \) ; \n \n / ,
40- "" ,
41- ) ;
42- await fs . writeFile ( indexFilePath , updatedIndexContent , "utf8" ) ;
43-
44- const contextFilePath = path . join ( serverDir , "src/lib/context.ts" ) ;
45- const contextContent = await fs . readFile ( contextFilePath , "utf8" ) ;
46- const updatedContextContent = contextContent
47- . replace ( / i m p o r t { a u t h } f r o m " \. \/ a u t h " ; \n / , "" )
48- . replace (
49- / c o n s t s e s s i o n = a w a i t a u t h \. a p i \. g e t S e s s i o n \( { \n \s + h e a d e r s : h o n o \. r e q \. r a w \. h e a d e r s , \n \s + } \) ; / ,
50- "const session = null;" ,
51- ) ;
52- await fs . writeFile ( contextFilePath , updatedContextContent , "utf8" ) ;
53- } else if ( ! hasDatabase ) {
19+ return ;
20+ }
21+
22+ if ( ! hasDatabase ) {
5423 log . warn (
5524 pc . yellow (
5625 "Authentication enabled but no database selected. Auth will not function properly." ,
5726 ) ,
5827 ) ;
59- } else {
60- const envPath = path . join ( serverDir , ".env" ) ;
61- const templateEnvPath = path . join (
62- PKG_ROOT ,
63- getOrmTemplatePath (
64- options . orm ,
65- options . database ,
66- "packages/server/_env" ,
67- ) ,
68- ) ;
28+ return ;
29+ }
6930
70- if ( ! ( await fs . pathExists ( envPath ) ) ) {
71- if ( await fs . pathExists ( templateEnvPath ) ) {
72- await fs . copy ( templateEnvPath , envPath ) ;
73- } else {
74- const defaultEnv = `BETTER_AUTH_SECRET=${ generateAuthSecret ( ) }
31+ const envPath = path . join ( serverDir , ".env" ) ;
32+ const templateEnvPath = path . join (
33+ PKG_ROOT ,
34+ getOrmTemplatePath ( options . orm , options . database , "packages/server/_env" ) ,
35+ ) ;
36+
37+ if ( ! ( await fs . pathExists ( envPath ) ) ) {
38+ if ( await fs . pathExists ( templateEnvPath ) ) {
39+ await fs . copy ( templateEnvPath , envPath ) ;
40+ } else {
41+ const defaultEnv = `BETTER_AUTH_SECRET=${ generateAuthSecret ( ) }
7542BETTER_AUTH_URL=http://localhost:3000
7643CORS_ORIGIN=http://localhost:3001
7744${ options . database === "sqlite" ? "TURSO_CONNECTION_URL=http://127.0.0.1:8080" : "" }
7845${ options . orm === "prisma" ? 'DATABASE_URL="file:./dev.db"' : "" }
7946` ;
80- await fs . writeFile ( envPath , defaultEnv ) ;
81- }
82- } else {
83- let envContent = await fs . readFile ( envPath , "utf8" ) ;
84-
85- if ( ! envContent . includes ( "BETTER_AUTH_SECRET" ) ) {
86- envContent += `\nBETTER_AUTH_SECRET=${ generateAuthSecret ( ) } ` ;
87- }
47+ await fs . writeFile ( envPath , defaultEnv ) ;
48+ }
49+ } else {
50+ let envContent = await fs . readFile ( envPath , "utf8" ) ;
8851
89- if ( ! envContent . includes ( "BETTER_AUTH_URL " ) ) {
90- envContent += "\nBETTER_AUTH_URL=http://localhost:3000" ;
91- }
52+ if ( ! envContent . includes ( "BETTER_AUTH_SECRET " ) ) {
53+ envContent += `\nBETTER_AUTH_SECRET= ${ generateAuthSecret ( ) } ` ;
54+ }
9255
93- if ( ! envContent . includes ( "CORS_ORIGIN " ) ) {
94- envContent += "\nCORS_ORIGIN =http://localhost:3001 " ;
95- }
56+ if ( ! envContent . includes ( "BETTER_AUTH_URL " ) ) {
57+ envContent += "\nBETTER_AUTH_URL =http://localhost:3000 " ;
58+ }
9659
97- if (
98- options . database === "sqlite" &&
99- ! envContent . includes ( "TURSO_CONNECTION_URL" )
100- ) {
101- envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080" ;
102- }
60+ if ( ! envContent . includes ( "CORS_ORIGIN" ) ) {
61+ envContent += "\nCORS_ORIGIN=http://localhost:3001" ;
62+ }
10363
104- if ( options . orm === "prisma" && ! envContent . includes ( "DATABASE_URL" ) ) {
105- envContent += '\nDATABASE_URL="file:./dev.db"' ;
106- }
64+ if (
65+ options . database === "sqlite" &&
66+ ! envContent . includes ( "TURSO_CONNECTION_URL" )
67+ ) {
68+ envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080" ;
69+ }
10770
108- await fs . writeFile ( envPath , envContent ) ;
71+ if ( options . orm === "prisma" && ! envContent . includes ( "DATABASE_URL" ) ) {
72+ envContent += '\nDATABASE_URL="file:./dev.db"' ;
10973 }
11074
111- const clientEnvPath = path . join ( clientDir , ".env" ) ;
112- if ( ! ( await fs . pathExists ( clientEnvPath ) ) ) {
113- const clientEnvContent = "VITE_SERVER_URL=http://localhost:3000\n" ;
114- await fs . writeFile ( clientEnvPath , clientEnvContent ) ;
75+ await fs . writeFile ( envPath , envContent ) ;
76+ }
77+
78+ const clientEnvPath = path . join ( clientDir , ".env" ) ;
79+ if ( ! ( await fs . pathExists ( clientEnvPath ) ) ) {
80+ const clientEnvContent = "VITE_SERVER_URL=http://localhost:3000\n" ;
81+ await fs . writeFile ( clientEnvPath , clientEnvContent ) ;
82+ }
83+
84+ if ( options . orm === "prisma" ) {
85+ const packageJsonPath = path . join ( projectDir , "package.json" ) ;
86+ if ( await fs . pathExists ( packageJsonPath ) ) {
87+ const packageJson = await fs . readJson ( packageJsonPath ) ;
88+
89+ packageJson . scripts [ "prisma:generate" ] =
90+ "cd packages/server && npx prisma generate" ;
91+ packageJson . scripts [ "prisma:push" ] =
92+ "cd packages/server && npx prisma db push" ;
93+ packageJson . scripts [ "prisma:studio" ] =
94+ "cd packages/server && npx prisma studio" ;
95+ packageJson . scripts [ "db:setup" ] =
96+ "npm run auth:generate && npm run prisma:generate && npm run prisma:push" ;
97+
98+ await fs . writeJson ( packageJsonPath , packageJson , { spaces : 2 } ) ;
11599 }
100+ } else if ( options . orm === "drizzle" ) {
101+ const packageJsonPath = path . join ( projectDir , "package.json" ) ;
102+ if ( await fs . pathExists ( packageJsonPath ) ) {
103+ const packageJson = await fs . readJson ( packageJsonPath ) ;
104+
105+ packageJson . scripts [ "db:push" ] =
106+ "cd packages/server && npx @better-auth/cli migrate" ;
107+ packageJson . scripts [ "db:setup" ] =
108+ "npm run auth:generate && npm run db:push" ;
116109
117- if ( options . orm === "prisma" ) {
118- const prismaAuthPath = path . join ( serverDir , "src/lib/auth.ts" ) ;
119- const defaultPrismaAuthPath = path . join (
120- PKG_ROOT ,
121- getOrmTemplatePath (
122- options . orm ,
123- options . database ,
124- "packages/server/src/lib/auth.ts" ,
125- ) ,
126- ) ;
127-
128- if (
129- ( await fs . pathExists ( defaultPrismaAuthPath ) ) &&
130- ! ( await fs . pathExists ( prismaAuthPath ) )
131- ) {
132- await fs . ensureDir ( path . dirname ( prismaAuthPath ) ) ;
133- await fs . copy ( defaultPrismaAuthPath , prismaAuthPath ) ;
134- }
135-
136- let authContent = await fs . readFile ( prismaAuthPath , "utf8" ) ;
137- if ( ! authContent . includes ( "trustedOrigins" ) ) {
138- authContent = authContent . replace (
139- "export const auth = betterAuth({" ,
140- "export const auth = betterAuth({\n trustedOrigins: [process.env.CORS_ORIGIN!]," ,
141- ) ;
142- await fs . writeFile ( prismaAuthPath , authContent ) ;
143- }
144-
145- const packageJsonPath = path . join ( projectDir , "package.json" ) ;
146- if ( await fs . pathExists ( packageJsonPath ) ) {
147- const packageJson = await fs . readJson ( packageJsonPath ) ;
148-
149- packageJson . scripts [ "prisma:generate" ] =
150- "cd packages/server && npx prisma generate" ;
151- packageJson . scripts [ "prisma:push" ] =
152- "cd packages/server && npx prisma db push" ;
153- packageJson . scripts [ "prisma:studio" ] =
154- "cd packages/server && npx prisma studio" ;
155- packageJson . scripts [ "db:setup" ] =
156- "npm run auth:generate && npm run prisma:generate && npm run prisma:push" ;
157-
158- await fs . writeJson ( packageJsonPath , packageJson , { spaces : 2 } ) ;
159- }
160- } else if ( options . orm === "drizzle" ) {
161- const drizzleAuthPath = path . join ( serverDir , "src/lib/auth.ts" ) ;
162- const defaultDrizzleAuthPath = path . join (
163- PKG_ROOT ,
164- getOrmTemplatePath (
165- options . orm ,
166- options . database ,
167- "packages/server/src/lib/auth.ts" ,
168- ) ,
169- ) ;
170-
171- if (
172- ( await fs . pathExists ( defaultDrizzleAuthPath ) ) &&
173- ! ( await fs . pathExists ( drizzleAuthPath ) )
174- ) {
175- await fs . ensureDir ( path . dirname ( drizzleAuthPath ) ) ;
176- await fs . copy ( defaultDrizzleAuthPath , drizzleAuthPath ) ;
177- }
178-
179- const packageJsonPath = path . join ( projectDir , "package.json" ) ;
180- if ( await fs . pathExists ( packageJsonPath ) ) {
181- const packageJson = await fs . readJson ( packageJsonPath ) ;
182-
183- packageJson . scripts [ "db:push" ] =
184- "cd packages/server && npx @better-auth/cli migrate" ;
185- packageJson . scripts [ "db:setup" ] =
186- "npm run auth:generate && npm run db:push" ;
187-
188- await fs . writeJson ( packageJsonPath , packageJson , { spaces : 2 } ) ;
189- }
110+ await fs . writeJson ( packageJsonPath , packageJson , { spaces : 2 } ) ;
190111 }
191112 }
192113 } catch ( error ) {
0 commit comments