Skip to content

Commit 8185f30

Browse files
authored
fix: implemented reCaptcha Site key setup and added docs (PalisadoesFoundation#1446)
1 parent e2a3541 commit 8185f30

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

INSTALLATION.md

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ This `.env` file must be populated with the following environment variables for
261261
| REFRESH_TOKEN_SECRET | Used for signing/verifying JWT tokens |
262262
| MONGO_DB_URL | Used for connecting talawa-api to the mongoDB database |
263263
| RECAPTCHA_SECRET_KEY | Used for authentication using reCAPTCHA |
264+
| RECAPTCHA_SITE_KEY | Used for authentication using reCAPTCHA |
264265
| MAIL_USERNAME | Used for mailing service |
265266
| MAIL_PASSWORD | Used for mailing service |
266267
| LAST_RESORT_SUPERADMIN_EMAIL | Used for promoting the default super admin |

setup.ts

+42
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,37 @@ async function recaptcha(): Promise<void> {
138138
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
139139
}
140140
}
141+
async function recaptchaSiteKey(): Promise<void> {
142+
console.log(
143+
"\nPlease visit this URL to set up reCAPTCHA:\n\nhttps://www.google.com/recaptcha/admin/create"
144+
);
145+
console.log(
146+
'\nSelect reCAPTCHA v2 and the "I`m not a robot" checkbox option'
147+
);
148+
console.log(
149+
'\nAdd "localhost" in domains and accept the terms, then press submit'
150+
);
151+
152+
const { recaptchaSiteKey } = await inquirer.prompt([
153+
{
154+
type: "input",
155+
name: "recaptchaSiteKey",
156+
message: "Enter your reCAPTCHA site key:",
157+
validate: async (input: string): Promise<boolean | string> => {
158+
if (validateRecaptcha(input)) {
159+
return true;
160+
}
161+
return "Invalid reCAPTCHA site key. Please try again.";
162+
},
163+
},
164+
]);
165+
const config = dotenv.parse(fs.readFileSync(".env"));
166+
config.RECAPTCHA_SITE_KEY = recaptchaSiteKey;
167+
fs.writeFileSync(".env", "");
168+
for (const key in config) {
169+
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
170+
}
171+
}
141172

142173
function isValidEmail(email: string): boolean {
143174
const pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
@@ -350,6 +381,17 @@ async function main(): Promise<void> {
350381
await recaptcha();
351382
}
352383

384+
const { shouldSetRecaptchaSiteKey } = await inquirer.prompt({
385+
type: "confirm",
386+
name: "shouldSetRecaptchaSiteKey",
387+
message: "Would you like to set up a reCAPTCHA site key?",
388+
default: true,
389+
});
390+
391+
if (shouldSetRecaptchaSiteKey) {
392+
await recaptchaSiteKey();
393+
}
394+
353395
if (process.env.MAIL_USERNAME) {
354396
console.log(
355397
`\nMail username already exists with the value ${process.env.MAIL_USERNAME}`

0 commit comments

Comments
 (0)