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.
This commit is contained in:
Aaron William Po
2023-03-05 14:07:19 -05:00
parent f576f515a1
commit 584e3b349f
21 changed files with 5352 additions and 145 deletions

View File

@@ -11,10 +11,18 @@ import findUserByEmail from '@/services/User/findUserByEmail';
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import sendConfirmationEmail from '@/services/User/sendConfirmationEmail';
interface RegisterUserRequest extends NextApiRequest {
body: z.infer<typeof CreateUserValidationSchema>;
}
const { BASE_URL } = process.env;
if (!BASE_URL) {
throw new ServerError('BASE_URL env variable is not set.', 500);
}
const registerUser = async (req: RegisterUserRequest, res: NextApiResponse) => {
const [usernameTaken, emailTaken] = await Promise.all([
findUserByUsername(req.body.username),
@@ -41,11 +49,14 @@ const registerUser = async (req: RegisterUserRequest, res: NextApiResponse) => {
id: user.id,
username: user.username,
});
res.status(201).json({
message: 'User created successfully.',
payload: user,
statusCode: 201,
await sendConfirmationEmail(user);
res.status(200).json({
success: true,
statusCode: 200,
message: 'User registered successfully.',
payload: user,
});
};