♻️ refactor: update register.ts
swadhinbiswas committed 3 months ago
9989f4b
1 parent a198c73
commit 9989f4b
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/pages/api/auth/register.ts
+50-0
| @@ -18,6 +18,8 @@ import { applyRateLimit } from "@/middleware/rate-limit"; | ||
18 | 18 | import { RegisterUserSchema } from "@/lib/validation"; |
19 | 19 | import { z } from "zod"; |
20 | 20 | import { logger } from "@/lib/logger"; |
21 | +import { initRepository } from "@/lib/git"; | |
22 | +import { initRepoInStorage, finalizeRepoInit, isCloudStorage } from "@/lib/git-storage"; | |
21 | 23 | import { withErrorHandler } from "@/lib/errors"; |
22 | 24 | |
23 | 25 | const registerSchema = z.object({ |
| @@ -110,6 +112,54 @@ export const POST: APIRoute = withErrorHandler(async ({ request, cookies }) => { | ||
110 | 112 | updatedAt: timestamp, |
111 | 113 | }); |
112 | 114 | |
115 | + // Create profile repository (like GitHub's username/username repo) | |
116 | + try { | |
117 | + const profileRepoName = username; | |
118 | + const localGitPath = await initRepoInStorage(username, profileRepoName); | |
119 | + const readmeContent = `# ${displayName || username}\n\nWelcome to my profile!\n`; | |
120 | + | |
121 | + await initRepository(localGitPath, { | |
122 | + defaultBranch: "main", | |
123 | + readme: false, | |
124 | + repoName: profileRepoName, | |
125 | + ownerName: displayName || username, | |
126 | + }); | |
127 | + | |
128 | + // Insert profile repo into database | |
129 | + const profileRepoId = generateId("repo"); | |
130 | + const timestamp2 = new Date(); | |
131 | + await db.insert(schema.repositories).values({ | |
132 | + id: profileRepoId, | |
133 | + ownerId: userId, | |
134 | + name: profileRepoName, | |
135 | + slug: profileRepoName, | |
136 | + description: `${username}'s profile`, | |
137 | + visibility: "public", | |
138 | + defaultBranch: "main", | |
139 | + diskPath: `repos/${username}/${profileRepoName}.git`, | |
140 | + httpCloneUrl: "", | |
141 | + sshCloneUrl: "", | |
142 | + createdAt: timestamp2, | |
143 | + updatedAt: timestamp2, | |
144 | + }); | |
145 | + | |
146 | + // Commit the README | |
147 | + const { commitFile } = await import("@/lib/git"); | |
148 | + await commitFile(localGitPath, "main", "README.md", readmeContent, "Initial commit", { | |
149 | + name: displayName || username, | |
150 | + email: email, | |
151 | + }); | |
152 | + | |
153 | + // Upload to cloud if needed | |
154 | + if (await isCloudStorage()) { | |
155 | + finalizeRepoInit(username, profileRepoName).catch(() => {}); | |
156 | + } | |
157 | + | |
158 | + logger.info({ userId, profileRepoId }, "Profile repository created"); | |
159 | + } catch (err) { | |
160 | + logger.error({ err, userId }, "Failed to create profile repository (non-fatal)"); | |
161 | + } | |
162 | + | |
113 | 163 | // Create session |
114 | 164 | const userAgent = request.headers.get("User-Agent") || undefined; |
115 | 165 | const session = await createSession(userId, userAgent); |
116 | 166 | |