Skip to content

Commit e37f776

Browse files
added syn clerk data with mongo database: Astro
1 parent 2032c7d commit e37f776

7 files changed

Lines changed: 474 additions & 127 deletions

File tree

lib/authentication/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.`));

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/clerknuxt.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { nuxtMiddlewareContent, SignIn, SignUp } from "./utility.js";
1+
import { clerkUserController, clerkUserModel, clerkWebhookRoute, nuxtMiddlewareContent, SignIn, SignUp } from "./utility.js";
22
import chalk from "chalk";
33
import fs from "fs";
44
import { execa } from "execa";
55
import path from "path";
66

77
import inquirer from 'inquirer';
88

9-
export async function setupClerkNuxt(framework, projectName, language, styling, useTailwind) {
9+
export async function setupClerkNuxt(state) {
1010
try {
11+
const { framework, projectName, language, styling, uiLibrary, database } = state
12+
const useTailwind = (styling === "tailwind");
1113
console.log(chalk.blue(`Installing clerk package...`));
1214
const rootDir = path.resolve(process.cwd());
1315
await execa("npm", ["install", "@clerk/nuxt"], { cwd: rootDir, stdio: "inherit" });
@@ -30,12 +32,12 @@ export async function setupClerkNuxt(framework, projectName, language, styling,
3032
if (!envcontent.includes("PUBLIC_CLERK_PUBLISHABLE_KEY")) {
3133
envcontent += `\n\n${clerkEnvVars}`;
3234
fs.writeFileSync(envfilePath, envcontent.trim() + "\n", "utf-8");
33-
console.log(`Clerk environment variables added to ${file}`);
35+
console.log(`Clerk environment variables added to ${file}`);
3436
} else {
35-
console.log(`ℹ️ Clerk environment variables already exist in ${file}`);
37+
console.log(`Clerk environment variables already exist in ${file}`);
3638
}
3739
} catch (err) {
38-
console.error(`Error updating ${file}:`, err.message);
40+
console.error(`Error updating ${file}:`, err.message);
3941
}
4042
}
4143

@@ -45,7 +47,7 @@ export async function setupClerkNuxt(framework, projectName, language, styling,
4547

4648
// Check if @clerk/nuxt is already added
4749
if (config.includes("'@clerk/nuxt'") || config.includes('"@clerk/nuxt"')) {
48-
console.log("'@clerk/nuxt' is already present in modules.");
50+
console.log("'@clerk/nuxt' is already present in modules.");
4951
return;
5052
}
5153

@@ -110,9 +112,9 @@ import { SignedIn, SignedOut, SignInButton, UserButton } from "@clerk/nuxt/compo
110112
// Avoid duplicate imports
111113
if (!innerContent.includes("@clerk/nuxt/components")) {
112114
innerContent = clerkImportLine + "\n" + innerContent.trim();
113-
console.log(chalk.blue(" Clerk import added to existing <script setup>"));
115+
console.log(chalk.blue(" Clerk import added to existing <script setup>"));
114116
} else {
115-
console.log(chalk.green("ℹ️ Clerk import already present"));
117+
console.log(chalk.green(" Clerk import already present"));
116118
}
117119

118120
// Reconstruct script setup block with original lang
@@ -128,15 +130,65 @@ import { SignedIn, SignedOut, SignInButton, UserButton } from "@clerk/nuxt/compo
128130
// 5️⃣ Insert header before closing </template> if template exists
129131
if (homecontent.includes("</template>")) {
130132
homecontent = homecontent.replace("</template>", `${headerTemplate}\n</template>`);
131-
console.log(chalk.green("Header added to template"));
133+
console.log(chalk.green("Header added to template"));
132134
} else {
133-
console.log(chalk.yellow("⚠️ No </template> found — adding header at end of file"));
135+
console.log(chalk.yellow("No </template> found — adding header at end of file"));
134136
homecontent += `\n${headerTemplate}`;
135137
}
136138

137139
// 6️⃣ Write back updated file
138140
fs.writeFileSync(homePagePath, homecontent, "utf8");
139141

142+
if (database === "mongodb") {
143+
console.log(chalk.yellow("Setting up Syncing clerk data with database...."));
144+
const clerkSecretVars = "CLERK_WEBHOOK_SIGNING_SECRET=";
145+
for (const file of envFiles) {
146+
const envfilePath = path.resolve(file);
147+
try {
148+
let envcontent = "";
149+
150+
// If file exists, read it; otherwise create a new one
151+
if (fs.existsSync(envfilePath)) {
152+
envcontent = fs.readFileSync(envfilePath, "utf-8");
153+
}
154+
155+
// Prevent duplicate entries (idempotent)
156+
if (!envcontent.includes("CLERK_WEBHOOK_SIGNING_SECRET")) {
157+
envcontent += `\n\n${clerkSecretVars}`;
158+
fs.writeFileSync(envfilePath, envcontent.trim() + "\n", "utf-8");
159+
console.log(` Clerk environment variables added to ${file}`);
160+
} else {
161+
console.log(`Clerk environment variables already exist in ${file}`);
162+
}
163+
} catch (err) {
164+
console.error(` Error updating ${file}:`, err.message);
165+
}
166+
167+
168+
const webhookDir = path.join(srcDir, "pages", "api", "webhook");
169+
fs.mkdirSync(webhookDir, { recursive: true }); // ✅ creates folder if missing
170+
171+
const webhookPath = path.join(webhookDir, `clerk.${language === "ts" ? "ts" : "js"}`);
172+
const webhookContent = clerkWebhookRoute(framework, language);
173+
fs.writeFileSync(webhookPath, webhookContent);
174+
175+
const userModelDir = path.join(srcDir, "lib", "database", "models");
176+
fs.mkdirSync(userModelDir, { recursive: true }); // ✅ ensure folder exists
177+
const userModelPath = path.join(userModelDir, `user.model.${language === "ts" ? "ts" : "js"}`);
178+
const clerkModelContent = clerkUserModel(language);
179+
fs.writeFileSync(userModelPath, clerkModelContent);
180+
181+
const actionDir = path.join(srcDir, "lib", "actions");
182+
fs.mkdirSync(actionDir, { recursive: true }); // ✅ ensure folder exists
183+
const actionControllerPath = path.join(actionDir, `user.action.${language === "ts" ? "ts" : "js"}`);
184+
const actionControllerContent = clerkUserController(framework, language);
185+
fs.writeFileSync(actionControllerPath, actionControllerContent);
186+
187+
console.log(chalk.green("Successfully setup syncing data setup for clerk data with database!"));
188+
}
189+
}
190+
191+
140192
console.log(chalk.green("🎉 Successfully added '@clerk/nuxt'"));
141193
console.log(chalk.yellow('Update your NUXT_PUBLIC_CLERK_PUBLISHABLE_KEY and NUXT_CLERK_SECRET_KEY in .env.local'));
142194
console.log(chalk.yellow('Get your api by accessing official page of clerk.'));

lib/authentication/utils/clerkvue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export default router
151151
`;
152152
export async function setupVueClerk(state) {
153153
const daisy=state.uiLibrary==="daisyui";
154-
const { projectName, language } = state;
154+
const { projectName, language,styling,database } = state;
155155
console.log(chalk.blue(`Setting up Clerk for Vue project: ${projectName} (lang=${language})`));
156156

157157
const rootDir = path.isAbsolute(projectName)

0 commit comments

Comments
 (0)