Skip to content

Commit

Permalink
fix: updated setup.ts script to configure and verify SMTP credentials…
Browse files Browse the repository at this point in the history
… in env file during setup (PalisadoesFoundation#1647)

* feat: added smtp configurations for env

* feat: added smtp verification method in setup.ts

* feat: added error message during smtp configuration

* fix: fixed package-lock.json difference
  • Loading branch information
MahendraDani authored Jan 10, 2024
1 parent dc3f014 commit 5624556
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const inquirer = require("inquirer");
const mongodb = require("mongodb");
const redis = require("redis");
const { exec } = require("child_process");
const nodemailer = require("nodemailer");

dotenv.config();

Expand Down Expand Up @@ -405,6 +406,113 @@ async function importData(): Promise<void> {
}
}

type VerifySmtpConnectionReturnType = {
success: boolean;
error: any;
};
async function verifySmtpConnection(
config: Record<string, string>
): Promise<VerifySmtpConnectionReturnType> {
const transporter = nodemailer.createTransport({
host: config.SMTP_HOST,
port: Number(config.SMTP_PORT),
secure: config.SMTP_SSL_TLS === "true",
auth: {
user: config.SMTP_USERNAME,
pass: config.SMTP_PASSWORD,
},
});

try {
await transporter.verify();
console.log("SMTP connection verified successfully.");
return { success: true, error: null };
} catch (error: any) {
console.error("SMTP connection verification failed:");
return { success: false, error };
} finally {
transporter.close();
}
}
async function configureSmtp(): Promise<void> {
console.log(
"SMTP Configuration is necessary for sending Emails through Talawa"
);
const { shouldConfigureSmtp } = await inquirer.prompt({
type: "confirm",
name: "shouldConfigureSmtp",
message: "Would you like to configure SMTP for Talawa to send emails?",
default: true,
});

if (!shouldConfigureSmtp) {
console.log("SMTP configuration skipped.");
return;
}

const smtpConfig = await inquirer.prompt([
{
type: "input",
name: "SMTP_HOST",
message: "Enter SMTP host:",
},
{
type: "input",
name: "SMTP_PORT",
message: "Enter SMTP port:",
},
{
type: "input",
name: "SMTP_USERNAME",
message: "Enter SMTP username:",
},
{
type: "password",
name: "SMTP_PASSWORD",
message: "Enter SMTP password:",
},
{
type: "confirm",
name: "SMTP_SSL_TLS",
message: "Use SSL/TLS for SMTP?",
default: false,
},
]);

const isValidSmtpConfig =
smtpConfig.SMTP_HOST &&
smtpConfig.SMTP_PORT &&
smtpConfig.SMTP_USERNAME &&
smtpConfig.SMTP_PASSWORD;

if (!isValidSmtpConfig) {
console.error(
"Invalid SMTP configuration. Please provide all required parameters."
);
return;
}

const { success, error } = await verifySmtpConnection(smtpConfig);

if (!success) {
console.error(
"SMTP configuration verification failed. Please check your SMTP settings."
);
console.log(error.message);
return;
}

const config = dotenv.parse(fs.readFileSync(".env"));
config.IS_SMTP = "true";
Object.assign(config, smtpConfig);
fs.writeFileSync(".env", "");
for (const key in config) {
fs.appendFileSync(".env", `${key}=${config[key]}\n`);
}

console.log("SMTP configuration saved successfully.");
}

async function main(): Promise<void> {
console.log("Welcome to the Talawa API setup! 🚀");

Expand Down Expand Up @@ -522,6 +630,7 @@ async function main(): Promise<void> {
`\nMail username already exists with the value ${process.env.MAIL_USERNAME}`
);
}
await configureSmtp();
const { shouldSetMail } = await inquirer.prompt([
{
type: "confirm",
Expand Down

0 comments on commit 5624556

Please sign in to comment.