2024-10-04 23:14:40 -04:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import { response } from "@server/utils";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { User, emailVerificationCodes, users } from "@server/db/schema";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
import { isWithinExpirationDate } from "oslo";
|
2024-10-25 21:39:18 -04:00
|
|
|
import config from "@server/config";
|
2024-12-21 21:01:12 -05:00
|
|
|
import logger from "@server/logger";
|
2024-10-04 23:14:40 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
export const verifyEmailBody = z
|
|
|
|
.object({
|
|
|
|
code: z.string()
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-04 23:14:40 -04:00
|
|
|
|
|
|
|
export type VerifyEmailBody = z.infer<typeof verifyEmailBody>;
|
|
|
|
|
|
|
|
export type VerifyEmailResponse = {
|
|
|
|
valid: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function verifyEmail(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
2024-10-25 21:39:18 -04:00
|
|
|
next: NextFunction
|
2024-10-04 23:14:40 -04:00
|
|
|
): Promise<any> {
|
2024-10-25 21:39:18 -04:00
|
|
|
if (!config.flags?.require_email_verification) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Email verification is not enabled"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-04 23:14:40 -04:00
|
|
|
const parsedBody = verifyEmailBody.safeParse(req.body);
|
|
|
|
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-10-25 21:39:18 -04:00
|
|
|
fromError(parsedBody.error).toString()
|
|
|
|
)
|
2024-10-04 23:14:40 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { code } = parsedBody.data;
|
|
|
|
|
|
|
|
const user = req.user as User;
|
|
|
|
|
|
|
|
if (user.emailVerified) {
|
|
|
|
return next(
|
2024-10-25 21:39:18 -04:00
|
|
|
createHttpError(HttpCode.BAD_REQUEST, "Email is already verified")
|
2024-10-04 23:14:40 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const valid = await isValidCode(user, code);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
await db
|
|
|
|
.delete(emailVerificationCodes)
|
2024-10-13 17:13:47 -04:00
|
|
|
.where(eq(emailVerificationCodes.userId, user.userId));
|
2024-10-04 23:14:40 -04:00
|
|
|
|
|
|
|
await db
|
|
|
|
.update(users)
|
|
|
|
.set({
|
2024-12-21 21:01:12 -05:00
|
|
|
emailVerified: true
|
2024-10-04 23:14:40 -04:00
|
|
|
})
|
2024-10-13 17:13:47 -04:00
|
|
|
.where(eq(users.userId, user.userId));
|
2024-10-12 23:00:36 -04:00
|
|
|
} else {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-10-25 21:39:18 -04:00
|
|
|
"Invalid verification code"
|
|
|
|
)
|
2024-10-12 23:00:36 -04:00
|
|
|
);
|
2024-10-04 23:14:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return response<VerifyEmailResponse>(res, {
|
|
|
|
success: true,
|
|
|
|
error: false,
|
2024-10-12 23:00:36 -04:00
|
|
|
message: "Email verified",
|
2024-10-04 23:14:40 -04:00
|
|
|
status: HttpCode.OK,
|
|
|
|
data: {
|
2024-12-21 21:01:12 -05:00
|
|
|
valid
|
|
|
|
}
|
2024-10-04 23:14:40 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-12-21 21:01:12 -05:00
|
|
|
logger.error(error);
|
2024-10-04 23:14:40 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
2024-10-25 21:39:18 -04:00
|
|
|
"Failed to verify email"
|
|
|
|
)
|
2024-10-04 23:14:40 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default verifyEmail;
|
|
|
|
|
|
|
|
async function isValidCode(user: User, code: string): Promise<boolean> {
|
|
|
|
const codeRecord = await db
|
|
|
|
.select()
|
|
|
|
.from(emailVerificationCodes)
|
2024-10-13 17:13:47 -04:00
|
|
|
.where(eq(emailVerificationCodes.userId, user.userId))
|
2024-10-04 23:14:40 -04:00
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (user.email !== codeRecord[0].email) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codeRecord.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codeRecord[0].code !== code) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isWithinExpirationDate(new Date(codeRecord[0].expiresAt))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|