From 398559b6d4bdc7a97a42bd1ff365b6882278ad69 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 11 Oct 2024 13:48:00 -0400 Subject: [PATCH 1/3] Add nodemailer dependency. --- package-lock.json | 31 +++++++++++++++++++++++++++++++ package.json | 2 ++ 2 files changed, 33 insertions(+) diff --git a/package-lock.json b/package-lock.json index 8de19ed..8764fd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@types/express": "^4.17.13", "@types/express-session": "^1.17.4", "@types/jsonwebtoken": "^8.5.8", + "@types/nodemailer": "^6.4.16", "@types/supertest": "^6.0.2", "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^7.1.1", @@ -31,6 +32,7 @@ "jsonwebtoken": "^8.5.1", "mysql2": "^2.3.3", "nanoid": "^3.3.2", + "nodemailer": "^6.9.15", "path": "^0.12.7", "sequelize": "^6.37.1", "sha3": "^2.1.4", @@ -1468,6 +1470,14 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.2.tgz", "integrity": "sha512-KcfkBq9H4PI6Vpu5B/KoPeuVDAbmi+2mDBqGPGUgoL7yXQtcWGu2vJWmmRkneWK3Rh0nIAX192Aa87AqKHYChQ==" }, + "node_modules/@types/nodemailer": { + "version": "6.4.16", + "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.16.tgz", + "integrity": "sha512-uz6hN6Pp0upXMcilM61CoKyjT7sskBoOWpptkjjJp8jIMlTdc3xG01U7proKkXzruMS4hS0zqtHNkNPFB20rKQ==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", @@ -5656,6 +5666,14 @@ "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz", "integrity": "sha512-d9VeXT4SJ7ZeOqGX6R5EM022wpL+eWPooLI+5UpWn2jCT1aosUQEhQP214x33Wkwx3JQMvIm+tIoVOdodFS40g==" }, + "node_modules/nodemailer": { + "version": "6.9.15", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.15.tgz", + "integrity": "sha512-AHf04ySLC6CIfuRtRiEYtGEXgRfa6INgWGluDhnxTZhHSKvrBu7lc1VVchQ0d8nPc4cFaZoPq8vkyNoZr0TpGQ==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", @@ -8661,6 +8679,14 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.2.tgz", "integrity": "sha512-KcfkBq9H4PI6Vpu5B/KoPeuVDAbmi+2mDBqGPGUgoL7yXQtcWGu2vJWmmRkneWK3Rh0nIAX192Aa87AqKHYChQ==" }, + "@types/nodemailer": { + "version": "6.4.16", + "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.16.tgz", + "integrity": "sha512-uz6hN6Pp0upXMcilM61CoKyjT7sskBoOWpptkjjJp8jIMlTdc3xG01U7proKkXzruMS4hS0zqtHNkNPFB20rKQ==", + "requires": { + "@types/node": "*" + } + }, "@types/qs": { "version": "6.9.7", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz", @@ -11814,6 +11840,11 @@ "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz", "integrity": "sha512-d9VeXT4SJ7ZeOqGX6R5EM022wpL+eWPooLI+5UpWn2jCT1aosUQEhQP214x33Wkwx3JQMvIm+tIoVOdodFS40g==" }, + "nodemailer": { + "version": "6.9.15", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.15.tgz", + "integrity": "sha512-AHf04ySLC6CIfuRtRiEYtGEXgRfa6INgWGluDhnxTZhHSKvrBu7lc1VVchQ0d8nPc4cFaZoPq8vkyNoZr0TpGQ==" + }, "normalize-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", diff --git a/package.json b/package.json index 4601642..57a2835 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "@types/express": "^4.17.13", "@types/express-session": "^1.17.4", "@types/jsonwebtoken": "^8.5.8", + "@types/nodemailer": "^6.4.16", "@types/supertest": "^6.0.2", "@types/uuid": "^8.3.4", "@typescript-eslint/eslint-plugin": "^7.1.1", @@ -33,6 +34,7 @@ "jsonwebtoken": "^8.5.1", "mysql2": "^2.3.3", "nanoid": "^3.3.2", + "nodemailer": "^6.9.15", "path": "^0.12.7", "sequelize": "^6.37.1", "sha3": "^2.1.4", From 8cf24a1dc2d7cc3ec6c382966bfda1d169537c3e Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 11 Oct 2024 14:52:04 -0400 Subject: [PATCH 2/3] Add utility function for sending email. --- src/email.ts | 55 ++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/email.ts b/src/email.ts index 0861022..2a20cb7 100644 --- a/src/email.ts +++ b/src/email.ts @@ -1,27 +1,32 @@ -// import nodemailer from "nodemailer"; +import nodemailer, { SendMailOptions } from "nodemailer"; +import dotenv from "dotenv"; -// const transporter = nodemailer.createTransport({ -// host: "smtp.gmail.com", -// port: 587, -// secure: false, -// auth: { -// user: "carifio24@gmail.com", -// pass: "C@rifio00" -// } -// }); +dotenv.config(); -// export async function sendEmail(to: string, from: string, subject: string, body: string) { -// const options = { -// from: from, -// to: to, -// subject: subject, -// text: body -// }; -// transporter.sendMail(options, (error, info) => { -// if (error) { -// console.log(error); -// } else { -// console.log("Email sent: " + info.response); -// } -// }); -// } +const transporter = nodemailer.createTransport({ + service: process.env.EMAIL_SERVICE, + auth: { + user: process.env.EMAIL_USERNAME, + pass: process.env.EMAIL_PASSWORD, + }, +}); + +export interface SendEmailOptions { + to: string; + subject: string; + text: string; +} + +export async function sendEmail(options: SendEmailOptions) { + const sendOptions: SendMailOptions = { + ...options, + from: process.env.EMAIL_FROM, + }; + transporter.sendMail(sendOptions, (error, info) => { + if (error) { + console.log(error); + } else { + console.log("Email sent: " + info.response); + } + }); +} From 4774925eec97ffe432a292f352a6ff697178b4ee Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Fri, 11 Oct 2024 14:52:22 -0400 Subject: [PATCH 3/3] Send an email when an educator account is created. --- src/server.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index fb417f2..90e62f7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -70,6 +70,7 @@ import * as Either from "effect/Either"; import { setupApp } from "./app"; import { getAPIKey } from "./authorization"; import { Sequelize } from "sequelize"; +import { sendEmail } from "./email"; // TODO: Clean up these type definitions @@ -144,10 +145,24 @@ export function createApp(db: Sequelize): Express { result = SignUpResult.BadRequest; } const statusCode = SignUpResult.statusCode(result); + const success = SignUpResult.success(result); + + if (success) { + sendEmail({ + to: "cosmicds@cfa.harvard.edu", + subject: "Educator account created", + text: ` + Educator account created at ${Date()}: + Name: ${data.first_name} ${data.last_name} + Email: ${data.email} + `, + }) + .catch(error => console.log(error)); + } res.status(statusCode).json({ educator_info: data, status: result, - success: SignUpResult.success(result) + success, }); });