mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Implement comprehensive auth template system
This commit is contained in:
@@ -18,13 +18,14 @@ export async function setupAuth(
|
||||
projectDir: string,
|
||||
enableAuth: boolean,
|
||||
): Promise<void> {
|
||||
if (!enableAuth) {
|
||||
return;
|
||||
}
|
||||
|
||||
const serverDir = path.join(projectDir, "packages/server");
|
||||
const clientDir = path.join(projectDir, "packages/client");
|
||||
|
||||
try {
|
||||
if (!enableAuth) {
|
||||
return;
|
||||
}
|
||||
addPackageDependency({
|
||||
dependencies: ["better-auth"],
|
||||
projectDir: serverDir,
|
||||
|
||||
@@ -40,6 +40,65 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
|
||||
if (await fs.pathExists(ormTemplateDir)) {
|
||||
await fs.copy(ormTemplateDir, projectDir, { overwrite: true });
|
||||
|
||||
const serverSrcPath = path.join(projectDir, "packages/server/src");
|
||||
const baseLibPath = path.join(serverSrcPath, "lib");
|
||||
const withAuthLibPath = path.join(serverSrcPath, "with-auth-lib");
|
||||
|
||||
if (options.auth) {
|
||||
await fs.remove(baseLibPath);
|
||||
await fs.move(withAuthLibPath, baseLibPath);
|
||||
|
||||
if (options.orm === "prisma") {
|
||||
const schemaPath = path.join(
|
||||
projectDir,
|
||||
"packages/server/prisma/schema.prisma",
|
||||
);
|
||||
const withAuthSchemaPath = path.join(
|
||||
projectDir,
|
||||
"packages/server/prisma/with-auth-schema.prisma",
|
||||
);
|
||||
|
||||
if (await fs.pathExists(withAuthSchemaPath)) {
|
||||
await fs.remove(schemaPath);
|
||||
await fs.move(withAuthSchemaPath, schemaPath);
|
||||
}
|
||||
} else if (options.orm === "drizzle") {
|
||||
const schemaPath = path.join(
|
||||
projectDir,
|
||||
"packages/server/src/db/schema.ts",
|
||||
);
|
||||
const withAuthSchemaPath = path.join(
|
||||
projectDir,
|
||||
"packages/server/src/db/with-auth-schema.ts",
|
||||
);
|
||||
|
||||
if (await fs.pathExists(withAuthSchemaPath)) {
|
||||
await fs.remove(schemaPath);
|
||||
await fs.move(withAuthSchemaPath, schemaPath);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await fs.remove(withAuthLibPath);
|
||||
|
||||
if (options.orm === "prisma") {
|
||||
const withAuthSchema = path.join(
|
||||
projectDir,
|
||||
"packages/server/prisma/with-auth-schema.prisma",
|
||||
);
|
||||
if (await fs.pathExists(withAuthSchema)) {
|
||||
await fs.remove(withAuthSchema);
|
||||
}
|
||||
} else if (options.orm === "drizzle") {
|
||||
const withAuthSchema = path.join(
|
||||
projectDir,
|
||||
"packages/server/src/db/with-auth-schema.ts",
|
||||
);
|
||||
if (await fs.pathExists(withAuthSchema)) {
|
||||
await fs.remove(withAuthSchema);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +110,6 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
);
|
||||
|
||||
await setupAuth(projectDir, options.auth);
|
||||
|
||||
await setupEnvironmentVariables(projectDir, options);
|
||||
|
||||
if (options.git) {
|
||||
@@ -63,7 +121,6 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
}
|
||||
|
||||
await updatePackageConfigurations(projectDir, options);
|
||||
|
||||
await createReadme(projectDir, options);
|
||||
|
||||
displayPostInstallInstructions(
|
||||
|
||||
@@ -17,6 +17,10 @@ export async function setupEnvironmentVariables(
|
||||
envContent = await fs.readFile(envPath, "utf8");
|
||||
}
|
||||
|
||||
if (!envContent.includes("CORS_ORIGIN")) {
|
||||
envContent += "\nCORS_ORIGIN=http://localhost:3001";
|
||||
}
|
||||
|
||||
if (options.auth) {
|
||||
if (!envContent.includes("BETTER_AUTH_SECRET")) {
|
||||
envContent += `\nBETTER_AUTH_SECRET=${generateAuthSecret()}`;
|
||||
@@ -25,16 +29,6 @@ export async function setupEnvironmentVariables(
|
||||
if (!envContent.includes("BETTER_AUTH_URL")) {
|
||||
envContent += "\nBETTER_AUTH_URL=http://localhost:3000";
|
||||
}
|
||||
|
||||
if (!envContent.includes("CORS_ORIGIN")) {
|
||||
envContent += "\nCORS_ORIGIN=http://localhost:3001";
|
||||
}
|
||||
|
||||
const clientEnvPath = path.join(clientDir, ".env");
|
||||
if (!(await fs.pathExists(clientEnvPath))) {
|
||||
const clientEnvContent = "VITE_SERVER_URL=http://localhost:3000\n";
|
||||
await fs.writeFile(clientEnvPath, clientEnvContent);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.database !== "none") {
|
||||
@@ -54,4 +48,17 @@ export async function setupEnvironmentVariables(
|
||||
}
|
||||
|
||||
await fs.writeFile(envPath, envContent.trim());
|
||||
|
||||
const clientEnvPath = path.join(clientDir, ".env");
|
||||
let clientEnvContent = "";
|
||||
|
||||
if (await fs.pathExists(clientEnvPath)) {
|
||||
clientEnvContent = await fs.readFile(clientEnvPath, "utf8");
|
||||
}
|
||||
|
||||
if (!clientEnvContent.includes("VITE_SERVER_URL")) {
|
||||
clientEnvContent += "VITE_SERVER_URL=http://localhost:3000\n";
|
||||
}
|
||||
|
||||
await fs.writeFile(clientEnvPath, clientEnvContent.trim());
|
||||
}
|
||||
|
||||
@@ -33,32 +33,30 @@ function getDatabaseInstructions(
|
||||
const instructions = [];
|
||||
|
||||
if (orm === "prisma") {
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Apply schema: ${pc.dim(`${runCmd} db:push`)}`,
|
||||
);
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Database UI: ${pc.dim(`${runCmd} db:studio`)}`,
|
||||
);
|
||||
|
||||
if (database === "sqlite") {
|
||||
instructions.push(
|
||||
`${pc.yellow("NOTE:")} Turso support with Prisma is in Early Access and requires additional setup.`,
|
||||
`${pc.dim("Learn more at: https://www.prisma.io/docs/orm/overview/databases/turso")}`,
|
||||
);
|
||||
}
|
||||
} else if (orm === "drizzle") {
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Apply schema: ${pc.dim(`${runCmd} db:push`)}`,
|
||||
);
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Database UI: ${pc.dim(`${runCmd} db:studio`)}`,
|
||||
);
|
||||
|
||||
} else if (orm === "drizzle") {
|
||||
if (database === "sqlite") {
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Start local DB: ${pc.dim(`cd packages/server && ${runCmd} db:local`)}`,
|
||||
);
|
||||
}
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Apply schema: ${pc.dim(`${runCmd} db:push`)}`,
|
||||
);
|
||||
instructions.push(
|
||||
`${pc.cyan("•")} Database UI: ${pc.dim(`${runCmd} db:studio`)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return instructions.length
|
||||
|
||||
Reference in New Issue
Block a user