Skip to content

Commit

Permalink
Add example sending email code via resend
Browse files Browse the repository at this point in the history
  • Loading branch information
rmarscher committed Oct 27, 2023
1 parent 936db95 commit 520ace8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/api/src/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,28 @@ export const sendEmailSignIn = async (ctx: ApiContextProps, email: string) => {
}).onConflictDoUpdate({ target: VerificationCodeTable.userId, set: props })
const resetLink = ctx.env.APP_URL + '/password-reset/update-password?code=' + encodeURIComponent(props.code) + '&email=' + encodeURIComponent(email)

// TODO send email
if (ctx.env.RESEND_API_KEY) {
// send email
const sendResult = await fetch('https://api.resend.com/emails', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${ctx.env.RESEND_API_KEY}`
},
body: JSON.stringify({
from: ctx.env.NEXT_PUBLIC_SUPPORT_EMAIL,
to: email,
subject: 'T4 App verification code',
html: `<p>This email was used to sign in to ${ctx.env.APP_URL}.</p>
<p><a href="${resetLink.replaceAll('&', '&amp;')}">Click here to continue to sign in.</a></p>
<p>Code: ${props.code}</p>
<p>If you did not request this code, you can ignore this email or reply to let us know.</p>
`
})
});
// TODO check sendResult response for success
return { success: true }
}
throw new TRPCError({ message: `Sending the access code ${props.code} to ${email} has not been implemented. It should email a link to ${resetLink}.`, code: "METHOD_NOT_SUPPORTED" })
}

Expand Down

0 comments on commit 520ace8

Please sign in to comment.