Skip to content

Commit a402be5

Browse files
Merge pull request #7 from YashVerma-code/conflictfix
Conflictfix
2 parents 92e5e1f + 808aeec commit a402be5

20 files changed

Lines changed: 1268 additions & 568 deletions

File tree

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ delete
33
.hackpack-state.json
44
महत्वपूर्ण.md
55
activate-hackpack.bat
6-
deactivate-hackpack.bat
6+
deactivate-hackpack.bat
7+
test1/
8+
test2/
9+
test3/
10+
test4/
11+
test5/
12+
backend/

lib/authentication/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export async function setupClerk(state) {
264264
return;
265265
}
266266
if (framework === 'next') {
267-
await setupNextClerk(framework, projectName, language, styling,useTailwind);
267+
await setupNextClerk(state);
268268
return;
269269
} else if (framework === 'vue') {
270270
// Delegate Vue-specific Clerk setup to helper
@@ -279,10 +279,10 @@ export async function setupClerk(state) {
279279
await setupViteReactClerk(framework, projectName, language, styling,useTailwind, state.uiLibrary);
280280
return;
281281
} else if (framework === 'astro') {
282-
await setupClerkAstro(framework, projectName, language, styling,useTailwind);
282+
await setupClerkAstro(state);
283283
return;
284284
} else if (framework === "nuxt") {
285-
await setupClerkNuxt(framework, projectName, language, styling,useTailwind);
285+
await setupClerkNuxt(state);
286286
return;
287287
} else {
288288
console.log(chalk.yellow(`clerk setup for framework '${framework}' is not yet automated. Please refer to https://clerk.com/docs for manual setup instructions.`));
@@ -293,7 +293,7 @@ export async function setupClerk(state) {
293293
export async function setupAuth0(state) {
294294
const { framework, projectName, language } = state;
295295

296-
console.log(chalk.blue(`Setting up Auth0 for ${framework} project: ${projectName} \n📝 Language: ${language}`));
296+
console.log(chalk.blue(`Setting up Auth0 for ${framework} project: ${projectName} \nLanguage: ${language}`));
297297

298298
// Validate framework support
299299
if (!isFrameworkSupported(framework)) {
@@ -305,6 +305,6 @@ export async function setupAuth0(state) {
305305
await setupAuth0Angular(state);
306306
return;
307307
} else {
308-
console.log(chalk.yellow(`⚠️ Auth0 setup for framework '${framework}' is not yet automated. Please refer to https://auth0.com/docs/quickstart/spa/angular for manual setup instructions.`));
308+
console.log(chalk.yellow(`Auth0 setup for framework '${framework}' is not yet automated. Please refer to https://auth0.com/docs/quickstart/spa/angular for manual setup instructions.`));
309309
}
310310
}

lib/authentication/utils/clerkastro.js

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { AstromiddlewareContent, clerkUI, SignInPage, SignUpPage } from "./utility.js";
1+
import { AstromiddlewareContent, clerkUI, clerkUserController, clerkUserModel, clerkWebhookRoute, SignInPage, SignUpPage } from "./utility.js";
22
import chalk from "chalk";
33
import fs from "fs";
44
import { execa } from "execa";
55
import path from "path";
6-
import inquirer from 'inquirer';
76

8-
export async function setupClerkAstro(framework, projectName, language, styling, useTailwind) {
7+
export async function setupClerkAstro(state) {
8+
const { framework, projectName, language, styling, uiLibrary, database } = state
9+
const useTailwind = (styling === "tailwind");
910
try {
1011
const targetDir = path.resolve(projectName);
1112
const currentDir = process.cwd();
@@ -39,12 +40,12 @@ export async function setupClerkAstro(framework, projectName, language, styling,
3940
if (!envcontent.includes("PUBLIC_CLERK_PUBLISHABLE_KEY")) {
4041
envcontent += `\n\n${clerkEnvVars}`;
4142
fs.writeFileSync(envfilePath, envcontent.trim() + "\n", "utf-8");
42-
console.log(`Clerk environment variables added to ${file}`);
43+
console.log(`Clerk environment variables added to ${file}`);
4344
} else {
44-
console.log(`ℹ️ Clerk environment variables already exist in ${file}`);
45+
console.log(`Clerk environment variables already exist in ${file}`);
4546
}
4647
} catch (err) {
47-
console.error(`Error updating ${file}:`, err.message);
48+
console.error(`Error updating ${file}:`, err.message);
4849
}
4950
}
5051

@@ -112,9 +113,9 @@ export async function setupClerkAstro(framework, projectName, language, styling,
112113
}
113114

114115
fs.writeFileSync(configfilePath, content, "utf-8");
115-
console.log("astro.config.mjs updated successfully!");
116+
console.log("astro.config.mjs updated successfully!");
116117
} catch (err) {
117-
console.error("Error updating astro.config.mjs:", err.message);
118+
console.error("Error updating astro.config.mjs:", err.message);
118119
}
119120

120121
const homefilePath = path.join(srcDir, 'pages', "index.astro");; // Path to your file
@@ -143,13 +144,13 @@ export async function setupClerkAstro(framework, projectName, language, styling,
143144
// Write the updated content back to the file
144145
fs.writeFileSync(homefilePath, updatedContent, "utf-8");
145146

146-
console.log("Clerk UI added successfully to test.astro!");
147+
console.log("Clerk UI added successfully to test.astro!");
147148

148149
// Write middleware file
149150
const middlewareFile = path.join(srcDir, `middleware.${(language === "ts") ? `ts` : `js`}`);
150151
fs.writeFileSync(middlewareFile, AstromiddlewareContent.trim());
151152

152-
console.log(chalk.green("Middleware file created at:"), middlewareFile);
153+
console.log(chalk.green("Middleware file created at:"), middlewareFile);
153154

154155
const signinpageContent = SignInPage(useTailwind);
155156
const signuppageContent = SignUpPage(useTailwind);
@@ -160,13 +161,61 @@ export async function setupClerkAstro(framework, projectName, language, styling,
160161
const signUpPagePath = path.join(srcDir, "pages", "signup.astro");
161162
fs.writeFileSync(signUpPagePath, signuppageContent);
162163

164+
if (database === "mongodb") {
165+
console.log(chalk.yellow("Setting up Syncing clerk data with database...."));
166+
const clerkSecretVars = "CLERK_WEBHOOK_SIGNING_SECRET=";
167+
for (const file of envFiles) {
168+
const envfilePath = path.resolve(file);
169+
try {
170+
let envcontent = "";
171+
172+
// If file exists, read it; otherwise create a new one
173+
if (fs.existsSync(envfilePath)) {
174+
envcontent = fs.readFileSync(envfilePath, "utf-8");
175+
}
176+
177+
// Prevent duplicate entries (idempotent)
178+
if (!envcontent.includes("CLERK_WEBHOOK_SIGNING_SECRET")) {
179+
envcontent += `\n\n${clerkSecretVars}`;
180+
fs.writeFileSync(envfilePath, envcontent.trim() + "\n", "utf-8");
181+
console.log(` Clerk environment variables added to ${file}`);
182+
} else {
183+
console.log(`Clerk environment variables already exist in ${file}`);
184+
}
185+
} catch (err) {
186+
console.error(` Error updating ${file}:`, err.message);
187+
}
188+
189+
190+
const webhookDir = path.join(srcDir, "pages", "api", "webhook");
191+
fs.mkdirSync(webhookDir, { recursive: true }); // ✅ creates folder if missing
192+
193+
const webhookPath = path.join(webhookDir, `clerk.${language === "ts" ? "ts" : "js"}`);
194+
const webhookContent = clerkWebhookRoute(framework, language);
195+
fs.writeFileSync(webhookPath, webhookContent);
196+
197+
const userModelDir = path.join(srcDir, "lib", "database", "models");
198+
fs.mkdirSync(userModelDir, { recursive: true }); // ✅ ensure folder exists
199+
const userModelPath = path.join(userModelDir, `user.model.${language === "ts" ? "ts" : "js"}`);
200+
const clerkModelContent = clerkUserModel(language);
201+
fs.writeFileSync(userModelPath, clerkModelContent);
202+
203+
const actionDir = path.join(srcDir, "lib", "actions");
204+
fs.mkdirSync(actionDir, { recursive: true }); // ✅ ensure folder exists
205+
const actionControllerPath = path.join(actionDir, `user.action.${language === "ts" ? "ts" : "js"}`);
206+
const actionControllerContent = clerkUserController(framework, language);
207+
fs.writeFileSync(actionControllerPath, actionControllerContent);
208+
209+
console.log(chalk.green("Successfully setup syncing data setup for clerk data with database!"));
210+
}
211+
}
163212

164-
console.log(chalk.green.bold('\n🎉 Astro Clerk setup completed!'));
213+
console.log(chalk.green.bold('\nAstro Clerk setup completed!'));
165214
console.log(chalk.yellow('Update your PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY in .env.local'));
166215
console.log(chalk.yellow('Get your api by accessing official page of clerk.'));
167216

168217
} catch (err) {
169-
console.error(chalk.red("Error setting up Clerk for authentication"), err.message);
218+
console.error(chalk.red("Error setting up Clerk for authentication"), err.message);
170219
console.log(chalk.yellow("You may need to finish clerk setup manually."));
171220
}
172221

lib/authentication/utils/clerknext.js

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { newStyles, nextMiddleware, nextsignInCss, nextSignInPage, nextsignUpCss, nextSignUpPage } from "./utility.js";
1+
import { newStyles, nextMiddleware, nextsignInCss, nextSignInPage, nextsignUpCss, nextSignUpPage, clerkUserModel, clerkUserController, clerkWebhookRoute } from "./utility.js";
22
import chalk from "chalk";
33
import fs from "fs";
44
import { execa } from "execa";
55
import path from "path";
66

7-
export async function setupNextClerk(framework, projectName, language, styling,useTailwind) {
7+
export async function setupNextClerk(state) {
8+
const { projectName, language, styling, database} = state;
9+
const useTailwind = (styling === "tailwind");
810
try {
911
const targetDir = path.resolve(projectName);
1012
const currentDir = process.cwd();
@@ -61,27 +63,69 @@ export async function setupNextClerk(framework, projectName, language, styling,u
6163
fs.writeFileSync(signupCssPath, signupCssContent);
6264
}
6365

64-
const middlewareContent = nextMiddleware;
66+
let middlewareContent = nextMiddleware;
67+
if (database === 'mongodb') {
68+
middlewareContent = middlewareContent.replace("'/sign-in(.*)','/sign-up(.*)'", "'/sign-in(.*)','/sign-up(.*)', '/api/webhooks(.*)',");
69+
}
6570
const middlewareFilePath = path.join(srcDir, `middleware.${language === 'ts' ? 'ts' : 'js'}`);
6671
fs.writeFileSync(middlewareFilePath, middlewareContent);
6772

73+
if (database === 'mongodb') {
74+
try {
75+
// webhook route
76+
const webhookDir = path.join(srcDir, 'app', 'api', 'webhooks', 'clerk');
77+
fs.mkdirSync(webhookDir, { recursive: true });
78+
const webhookExt = language === 'ts' ? 'ts' : 'js';
79+
const webhookPath = path.join(webhookDir, `route.${webhookExt}`);
80+
const webhookContent = clerkWebhookRoute ? clerkWebhookRoute(language, 'next') : '';
81+
if (webhookContent) fs.writeFileSync(webhookPath, webhookContent, 'utf8');
82+
83+
// user controller (actions)
84+
const actionsDir = path.join(rootDir, 'lib', 'actions');
85+
fs.mkdirSync(actionsDir, { recursive: true });
86+
const controllerExt = language === 'ts' ? 'ts' : 'js';
87+
const controllerPath = path.join(actionsDir, `user.actions.${controllerExt}`);
88+
const controllerContent = clerkUserController ? clerkUserController(language, 'next') : '';
89+
if (controllerContent) fs.writeFileSync(controllerPath, controllerContent, 'utf8');
90+
91+
// user model
92+
const modelsDir = path.join(rootDir, 'lib', 'database', 'models');
93+
fs.mkdirSync(modelsDir, { recursive: true });
94+
const modelExt = language === 'ts' ? 'ts' : 'js';
95+
const modelPath = path.join(modelsDir, `user.model.${modelExt}`);
96+
const modelContent = clerkUserModel ? clerkUserModel(language) : '';
97+
if (modelContent) fs.writeFileSync(modelPath, modelContent, 'utf8');
98+
99+
// Append webhook signing secret to .env.local if missing
100+
const envFile = path.join(rootDir, '.env.local');
101+
let envExisting = '';
102+
if (fs.existsSync(envFile)) envExisting = fs.readFileSync(envFile, 'utf8');
103+
const secretLine = 'CLERK_WEBHOOK_SIGNING_SECRET=your_secret';
104+
if (!envExisting.includes('CLERK_WEBHOOK_SIGNING_SECRET')) {
105+
fs.appendFileSync(envFile, `\n${secretLine}\n`, 'utf8');
106+
console.log(chalk.green('Added CLERK_WEBHOOK_SIGNING_SECRET to .env.local'));
107+
}
108+
} catch (e) {
109+
console.log(chalk.yellow('Could not scaffold Clerk MongoDB helpers automatically:'), e.message);
110+
}
111+
}
112+
68113
const envPath = path.join(rootDir, ".env.local");
69114

70-
// Content you want to ensure exists
71115
const newEnvContent = `
72116
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
73117
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
74118
NEXT_PUBLIC_CLERK_SIGN_IN_FALLBACK_REDIRECT_URL=/
75119
NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/
120+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_publishable_key_here
121+
CLERK_SECRET_KEY=your_secret_key_here
76122
`;
77123

78-
// Read existing content if file exists
79124
let existing = "";
80125
if (fs.existsSync(envPath)) {
81126
existing = fs.readFileSync(envPath, "utf-8");
82127
}
83128

84-
// Filter out only new lines that don't already exist
85129
const linesToAdd = newEnvContent
86130
.trim()
87131
.split("\n")
@@ -91,9 +135,9 @@ NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/
91135
// Append only new lines
92136
if (linesToAdd) {
93137
fs.appendFileSync(envPath, `\n${linesToAdd}\n`, "utf-8");
94-
console.log("Added missing env variables to .env.local successfully!");
138+
console.log("Added missing env variables to .env.local successfully!");
95139
} else {
96-
console.log("ℹ️ All Clerk environment variables already exist in .env.local.");
140+
console.log("All Clerk environment variables already exist in .env.local.");
97141
}
98142
const filePath = path.join(srcDir, `app`, `page.${language === 'ts' ? 'tsx' : 'js'}`);
99143
let content = fs.readFileSync(filePath, "utf8");
@@ -164,9 +208,9 @@ NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/
164208

165209
if (!existingCss.includes(styleClerkUi)) {
166210
fs.appendFileSync(targetPath, `\n${styleClerkUi}\n`, "utf-8");
167-
console.log(`Added user auth button styles to ${path.basename(targetPath)} successfully!`);
211+
console.log(`Added user auth button styles to ${path.basename(targetPath)} successfully!`);
168212
} else {
169-
console.log(`ℹ️ User auth button styles already exist in ${path.basename(targetPath)}, skipped adding again.`);
213+
console.log(`User auth button styles already exist in ${path.basename(targetPath)}, skipped adding again.`);
170214
}
171215
}
172216

@@ -184,7 +228,7 @@ NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/
184228

185229
// Add import if missing
186230
if (!layoutcontent.includes("ClerkProvider")) {
187-
const importLine = `import { ClerkProvider } from "@clerk/nextjs";\nimport { Toaster } from "@/components/ui/toaster";\n`;
231+
const importLine = `import { ClerkProvider } from "@clerk/nextjs";\n`;
188232
// place after the last import
189233
layoutcontent = layoutcontent.replace(/(import .*?;\n)(?!import)/s, match => match + importLine);
190234
layoutchanged = true;
@@ -207,11 +251,20 @@ NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/
207251

208252
console.log(chalk.green.bold(`Clerk setup completed successfully!\nYour app is now configured with Clerk authentication.\nRemember to add your Publishable Key in the .env file:\nVITE_CLERK_PUBLISHABLE_KEY=your_key_here\n\nYou can now start your app and enjoy secure authentication! 🚀`));
209253
console.log(chalk.yellow(
210-
"🛠️ First-time setup:\n" +
254+
"First-time setup:\n" +
211255
"1. Sign in to your Clerk dashboard.\n" +
212256
"2. Create a new application.\n" +
213257
"3. Copy your 'NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY' and 'CLERK_SECRET_KEY' into your .env.local file."
214258
));
259+
if(database === 'mongodb') {
260+
console.log(chalk.yellow(
261+
"4. Set up Clerk webhooks for user management:\n" +
262+
" - In the Clerk dashboard, go to Webhooks and create a new webhook with the URL: <HTTPS_URL>/api/webhooks/clerk\n" +
263+
" - Subscribe to events: user.created, user.updated, user.deleted\n" +
264+
" - Copy the webhook signing secret into your .env.local as:\n" +
265+
" CLERK_WEBHOOK_SIGNING_SECRET=your_secret_here"
266+
));
267+
}
215268

216269
} catch (error) {
217270
console.error(chalk.red("Error setting up Clerk for authentication"), error.message);

0 commit comments

Comments
 (0)