1
0

Forgot password functionality

This commit is contained in:
Ahmed Al-Taiar
2023-11-25 16:23:40 -05:00
parent de494cdc0a
commit 2dc6a867fb
3 changed files with 37 additions and 30 deletions

View File

@ -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).
Your browser should automatically open to [http://localhost:8910](http://localhost:8910).

View File

@ -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', '<br />')
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
},

30
api/src/lib/email.ts Normal file
View File

@ -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
}