From 2dc6a867fb8dd85db7ea37602a1f666e3760a6d6 Mon Sep 17 00:00:00 2001 From: Ahmed Al-Taiar Date: Sat, 25 Nov 2023 16:23:40 -0500 Subject: [PATCH] Forgot password functionality --- README.md | 3 ++- api/src/functions/auth.ts | 34 +++++----------------------------- api/src/lib/email.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 api/src/lib/email.ts diff --git a/README.md b/README.md index 3705d5d..bd938cf 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Built with [RedwoodJS](https://redwoodjs.com)! > - Redwood requires [Node.js](https://nodejs.org/en/) (=18.x) and [Yarn](https://yarnpkg.com/) (>=1.15) > - Are you on Windows? For best results, follow our [Windows development setup](https://redwoodjs.com/docs/how-to/windows-development-setup) guide > - A PostgreSQL database +> - A [Brevo](https://www.brevo.com/) account Start by installing dependencies: @@ -20,4 +21,4 @@ Then start the development server: yarn rw dev ``` -Your browser should automatically open to [http://localhost:8910](http://localhost:8910). \ No newline at end of file +Your browser should automatically open to [http://localhost:8910](http://localhost:8910). diff --git a/api/src/functions/auth.ts b/api/src/functions/auth.ts index 62aad69..592b58d 100644 --- a/api/src/functions/auth.ts +++ b/api/src/functions/auth.ts @@ -1,5 +1,4 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda' -import nodemailer from 'nodemailer' import { DbAuthHandler, @@ -8,6 +7,7 @@ import { } from '@redwoodjs/auth-dbauth-api' import { db } from 'src/lib/db' +import { sendEmail } from 'src/lib/email' export const handler = async ( event: APIGatewayProxyEvent, @@ -31,38 +31,14 @@ export const handler = async ( const domain = env == 'production' ? process.env.PROD_DOMAIN : process.env.DEV_DOMAIN - const messageContent = ` - Hello, you are receiving this email because a password reset was requested. - - Enter the following URL to begin resetting your password: - - ${domain}reset-password?reserToken=${user.resetToken} - - If this wasn't you, please disregard this email. + const text = `If this wasn't you, please disregard this email.\n\n\nHello ${user.firstName},\n\nYou are receiving this email because a password reset was requested.\nEnter the following URL to begin resetting your password:\n\n${domain}reset-password?resetToken=${user.resetToken} ` - const transporter = nodemailer.createTransport({ - host: 'smtp-relay.brevo.com', - port: 587, - secure: false, - auth: { - user: process.env.SEND_IN_BLUE_EMAIL, - pass: process.env.SEND_IN_BLUE_KEY, - }, - }) + const html = text.replaceAll('\n', '
') - transporter.verify((error, _success) => { - if (error) console.log(error) - }) + const subject = 'Password Reset Request' - const info = await transporter.sendMail({ - from: `"Parts Inventory (noreply)" \<${process.env.SEND_IN_BLUE_EMAIL}>`, - to: user.email, - subject: 'Account Password Reset Request', - text: messageContent, - }) - - console.log(info) + await sendEmail({ to: user.email, subject, text, html }) return user }, diff --git a/api/src/lib/email.ts b/api/src/lib/email.ts new file mode 100644 index 0000000..92a7f15 --- /dev/null +++ b/api/src/lib/email.ts @@ -0,0 +1,30 @@ +import * as nodemailer from 'nodemailer' + +interface Options { + to: string | string[] + subject: string + html: string + text: string +} + +export async function sendEmail({ to, subject, text, html }: Options) { + const transporter = nodemailer.createTransport({ + host: 'smtp-relay.brevo.com', + port: 587, + secure: false, + auth: { + user: process.env.SEND_IN_BLUE_EMAIL, + pass: process.env.SEND_IN_BLUE_KEY, + }, + }) + + const info = await transporter.sendMail({ + from: `"Parts Inventory (noreply)" \<${process.env.SEND_IN_BLUE_EMAIL}>`, + to: Array.isArray(to) ? to : [to], + subject, + text, + html, + }) + + return info +}