Files
the-biergarten-app/services/User/sendConfirmationEmail.ts
Aaron William Po 584e3b349f Implement confirm user functionality
This commit adds the necessary functionality to confirm a user's account.

It includes the addition of a new column in the user table to track whether an account is confirmed or not, and the implementation of JWT for confirmation tokens.

This commit integrates the SparkPost API as well as React Email to send dynamic emails for whatever purpose.

Upon user registration, a confirmation email will be sent to the user.
2023-03-13 22:35:57 -04:00

33 lines
1.0 KiB
TypeScript

import { generateConfirmationToken } from '@/config/jwt';
import sendEmail from '@/config/sparkpost/sendEmail';
import ServerError from '@/config/util/ServerError';
import Welcome from '@/emails/Welcome';
import { render } from '@react-email/render';
import { z } from 'zod';
import GetUserSchema from './schema/GetUserSchema';
const { BASE_URL } = process.env;
if (!BASE_URL) {
throw new ServerError('BASE_URL env variable is not set.', 500);
}
type UserSchema = z.infer<typeof GetUserSchema>;
const sendConfirmationEmail = async ({ id, username, email }: UserSchema) => {
const confirmationToken = generateConfirmationToken({ id, username });
const subject = 'Confirm your email';
const name = username;
const url = `${BASE_URL}/api/users/confirm?token=${confirmationToken}`;
const address = email;
const html = render(Welcome({ name, url, subject })!);
const text = render(Welcome({ name, url, subject })!, { plainText: true });
await sendEmail({ address, subject, text, html });
};
export default sendConfirmationEmail;