feat: add authentication in native

This commit is contained in:
Aman Varshney
2025-04-04 19:06:08 +05:30
parent ccc3ff2aa5
commit 81dc240e7b
24 changed files with 1119 additions and 717 deletions

View File

@@ -1,6 +1,7 @@
import path from "node:path";
import { log } from "@clack/prompts";
import pc from "picocolors";
import type { ProjectFrontend } from "../types";
import { addPackageDependency } from "../utils/add-package-deps";
export function generateAuthSecret(length = 32): string {
@@ -17,6 +18,7 @@ export function generateAuthSecret(length = 32): string {
export async function setupAuth(
projectDir: string,
enableAuth: boolean,
frontends: ProjectFrontend[] = [],
): Promise<void> {
if (!enableAuth) {
return;
@@ -24,16 +26,33 @@ export async function setupAuth(
const serverDir = path.join(projectDir, "apps/server");
const clientDir = path.join(projectDir, "apps/web");
const nativeDir = path.join(projectDir, "apps/native");
try {
addPackageDependency({
dependencies: ["better-auth"],
projectDir: serverDir,
});
addPackageDependency({
dependencies: ["better-auth"],
projectDir: clientDir,
});
if (
frontends.includes("react-router") ||
frontends.includes("tanstack-router")
) {
addPackageDependency({
dependencies: ["better-auth"],
projectDir: serverDir,
});
addPackageDependency({
dependencies: ["better-auth"],
projectDir: clientDir,
});
}
if (frontends.includes("native")) {
addPackageDependency({
dependencies: ["better-auth", "@better-auth/expo"],
projectDir: nativeDir,
});
addPackageDependency({
dependencies: ["better-auth", "@better-auth/expo"],
projectDir: serverDir,
});
}
} catch (error) {
log.error(pc.red("Failed to configure authentication"));
if (error instanceof Error) {

View File

@@ -66,7 +66,7 @@ export async function createProject(options: ProjectConfig): Promise<string> {
options.database,
options.frontend,
);
await setupAuth(projectDir, options.auth);
await setupAuth(projectDir, options.auth, options.frontend);
await setupRuntime(projectDir, options.runtime, options.backend);

View File

@@ -7,6 +7,7 @@ import type {
ProjectFrontend,
ProjectOrm,
} from "../types";
import { addPackageDependency } from "../utils/add-package-deps";
/**
* Copy base template structure but exclude app-specific folders that will be added based on options
@@ -233,10 +234,83 @@ export async function setupAuthTemplate(
if (await fs.pathExists(nativeAuthDir)) {
await fs.copy(nativeAuthDir, projectNativeDir, { overwrite: true });
}
addPackageDependency({
dependencies: ["@better-auth/expo"],
projectDir: path.join(projectDir, "apps/server"),
});
await updateAuthConfigWithExpoPlugin(projectDir, orm, database);
}
}
}
// Need to find a better way to handle this
async function updateAuthConfigWithExpoPlugin(
projectDir: string,
orm: ProjectOrm,
database: ProjectDatabase,
): Promise<void> {
const serverDir = path.join(projectDir, "apps/server");
let authFilePath: string | undefined;
if (orm === "drizzle") {
if (database === "sqlite") {
authFilePath = path.join(serverDir, "src/lib/auth.ts");
} else if (database === "postgres") {
authFilePath = path.join(serverDir, "src/lib/auth.ts");
}
} else if (orm === "prisma") {
if (database === "sqlite") {
authFilePath = path.join(serverDir, "src/lib/auth.ts");
} else if (database === "postgres") {
authFilePath = path.join(serverDir, "src/lib/auth.ts");
}
}
if (authFilePath && (await fs.pathExists(authFilePath))) {
let authFileContent = await fs.readFile(authFilePath, "utf8");
if (!authFileContent.includes("@better-auth/expo")) {
const importLine = 'import { expo } from "@better-auth/expo";\n';
const lastImportIndex = authFileContent.lastIndexOf("import");
const afterLastImport =
authFileContent.indexOf("\n", lastImportIndex) + 1;
authFileContent =
authFileContent.substring(0, afterLastImport) +
importLine +
authFileContent.substring(afterLastImport);
}
if (!authFileContent.includes("plugins:")) {
authFileContent = authFileContent.replace(
/}\);/,
" plugins: [expo()],\n});",
);
} else if (!authFileContent.includes("expo()")) {
authFileContent = authFileContent.replace(
/plugins: \[(.*?)\]/s,
(match, plugins) => {
return `plugins: [${plugins}${plugins.trim() ? ", " : ""}expo()]`;
},
);
}
if (!authFileContent.includes("my-better-t-app://")) {
authFileContent = authFileContent.replace(
/trustedOrigins: \[(.*?)\]/s,
(match, origins) => {
return `trustedOrigins: [${origins}${origins.trim() ? ", " : ""}"my-better-t-app://"]`;
},
);
}
await fs.writeFile(authFilePath, authFileContent);
}
}
export async function fixGitignoreFiles(projectDir: string): Promise<void> {
const gitignorePaths = await findGitignoreFiles(projectDir);