mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-20 18:54:52 +02:00
verify email workflow working
This commit is contained in:
parent
e176295593
commit
76eeb335a3
23 changed files with 16363 additions and 15802 deletions
50
server/routers/auth/requestEmailVerificationCode.ts
Normal file
50
server/routers/auth/requestEmailVerificationCode.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import { response } from "@server/utils";
|
||||
import { User } from "@server/db/schema";
|
||||
import { sendEmailVerificationCode } from "./sendEmailVerificationCode";
|
||||
|
||||
export type RequestEmailVerificationCodeResponse = {
|
||||
codeSent: boolean;
|
||||
};
|
||||
|
||||
export async function requestEmailVerificationCode(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
): Promise<any> {
|
||||
try {
|
||||
const user = req.user as User;
|
||||
|
||||
if (user.emailVerified) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
"Email is already verified",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await sendEmailVerificationCode(user.email, user.id);
|
||||
|
||||
return response<RequestEmailVerificationCodeResponse>(res, {
|
||||
data: {
|
||||
codeSent: true,
|
||||
},
|
||||
status: HttpCode.OK,
|
||||
success: true,
|
||||
error: false,
|
||||
message: `Email verification code sent to ${user.email}`,
|
||||
});
|
||||
} catch (error) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"Failed to send email verification code",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default requestEmailVerificationCode;
|
Loading…
Add table
Add a link
Reference in a new issue