Forgot password functionality
This commit is contained in:
@@ -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)
|
> - 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
|
> - 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 PostgreSQL database
|
||||||
|
> - A [Brevo](https://www.brevo.com/) account
|
||||||
|
|
||||||
Start by installing dependencies:
|
Start by installing dependencies:
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import type { APIGatewayProxyEvent, Context } from 'aws-lambda'
|
import type { APIGatewayProxyEvent, Context } from 'aws-lambda'
|
||||||
import nodemailer from 'nodemailer'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DbAuthHandler,
|
DbAuthHandler,
|
||||||
@@ -8,6 +7,7 @@ import {
|
|||||||
} from '@redwoodjs/auth-dbauth-api'
|
} from '@redwoodjs/auth-dbauth-api'
|
||||||
|
|
||||||
import { db } from 'src/lib/db'
|
import { db } from 'src/lib/db'
|
||||||
|
import { sendEmail } from 'src/lib/email'
|
||||||
|
|
||||||
export const handler = async (
|
export const handler = async (
|
||||||
event: APIGatewayProxyEvent,
|
event: APIGatewayProxyEvent,
|
||||||
@@ -31,38 +31,14 @@ export const handler = async (
|
|||||||
const domain =
|
const domain =
|
||||||
env == 'production' ? process.env.PROD_DOMAIN : process.env.DEV_DOMAIN
|
env == 'production' ? process.env.PROD_DOMAIN : process.env.DEV_DOMAIN
|
||||||
|
|
||||||
const messageContent = `
|
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}
|
||||||
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 transporter = nodemailer.createTransport({
|
const html = text.replaceAll('\n', '<br />')
|
||||||
host: 'smtp-relay.brevo.com',
|
|
||||||
port: 587,
|
|
||||||
secure: false,
|
|
||||||
auth: {
|
|
||||||
user: process.env.SEND_IN_BLUE_EMAIL,
|
|
||||||
pass: process.env.SEND_IN_BLUE_KEY,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
transporter.verify((error, _success) => {
|
const subject = 'Password Reset Request'
|
||||||
if (error) console.log(error)
|
|
||||||
})
|
|
||||||
|
|
||||||
const info = await transporter.sendMail({
|
await sendEmail({ to: user.email, subject, text, html })
|
||||||
from: `"Parts Inventory (noreply)" \<${process.env.SEND_IN_BLUE_EMAIL}>`,
|
|
||||||
to: user.email,
|
|
||||||
subject: 'Account Password Reset Request',
|
|
||||||
text: messageContent,
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log(info)
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
},
|
},
|
||||||
|
|||||||
30
api/src/lib/email.ts
Normal file
30
api/src/lib/email.ts
Normal 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user