mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
feat: create confirm user page, option to resend email if link expires
This commit is contained in:
@@ -21,16 +21,16 @@ const confirmUser = async (req: ConfirmUserRequest, res: NextApiResponse) => {
|
||||
const { token } = req.query;
|
||||
|
||||
const user = req.user!;
|
||||
const { id } = verifyConfirmationToken(token);
|
||||
const { id } = await verifyConfirmationToken(token);
|
||||
|
||||
if (user.accountIsVerified) {
|
||||
throw new ServerError('Your account is already verified.', 400);
|
||||
}
|
||||
|
||||
if (user.id !== id) {
|
||||
throw new ServerError('Could not confirm user.', 401);
|
||||
}
|
||||
|
||||
if (user.accountIsVerified) {
|
||||
throw new ServerError('User is already verified.', 400);
|
||||
}
|
||||
|
||||
await updateUserToBeConfirmedById(id);
|
||||
|
||||
res.status(200).json({
|
||||
|
||||
32
src/pages/api/users/resend-confirmation.ts
Normal file
32
src/pages/api/users/resend-confirmation.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
|
||||
import { UserExtendedNextApiRequest } from '@/config/auth/types';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiResponse } from 'next';
|
||||
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
|
||||
import { createRouter } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
import sendConfirmationEmail from '@/services/User/sendConfirmationEmail';
|
||||
|
||||
const resendConfirmation = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
const user = req.user!;
|
||||
|
||||
await sendConfirmationEmail(user);
|
||||
res.status(200).json({
|
||||
message: `Resent the confirmation email for ${user.username}.`,
|
||||
statusCode: 200,
|
||||
success: true,
|
||||
});
|
||||
};
|
||||
|
||||
const router = createRouter<
|
||||
UserExtendedNextApiRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
>();
|
||||
|
||||
router.post(getCurrentUser, resendConfirmation);
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
export default handler;
|
||||
Reference in New Issue
Block a user