diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 26c915ea..c6db3c33 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -59,7 +59,7 @@ export const signupUser = async (req: Request, res: Response) => { if (createdUser) { token = await userToken(createdUser.id as string, createdUser.email as string); } - const link: string = `${process.env.URL_HOST}:${process.env.PORT}/api/users/${token}/verify-email`; + const link: string = `${process.env.URL_HOST}/api/users/${token}/verify-email`; sendEmail('account_verify', { name: `${createdUser.firstName} ${createdUser.lastName}`, @@ -235,3 +235,38 @@ export const userVerify = async (req: Request, res: Response) => { } } }; +// Function for resend verification link +export const resendVerifyLink = async (req: Request, res: Response) => { + try { + const { email } = req.body; + + if (!validateEmail(email)) { + return res.status(400).json({ ok: false, error: 'Invalid email format' }); + } + if (validateFields(req, ['email']).length !== 0) { + res.status(400).json({ ok: false, error: 'Email is required' }); + return; + } + const user = await User.findOne({ where: { email } }); + if (!user) { + return res.status(400).json({ ok: false, error: 'User with this email does not exit, Sign up to continue' }); + } + const notVerifiedUser = await User.findOne({ where: { email, verified: false } }); + if (!notVerifiedUser) { + return res.status(202).json({ ok: false, error: `${email} is already verified. Login to continue` }); + } + + const token = await userToken(user.dataValues.id as string, user.dataValues.email as string); + const verificationLink: string = `${process.env.URL_HOST}/api/users/${token}/verify-email`; + + sendEmail('account_verify', { + name: `${user.dataValues.firstName} ${user.dataValues.lastName}`, + email, + link: verificationLink, + }); + res.status(201).json({ ok: true, message: 'Check your email to verify.' }); + } catch (error) { + logger.error('Resend-verify: ', error); + sendInternalErrorResponse(res, error); + } +}; diff --git a/src/docs/users.yaml b/src/docs/users.yaml index c1f4c00f..99c0ab00 100644 --- a/src/docs/users.yaml +++ b/src/docs/users.yaml @@ -160,5 +160,29 @@ paths: description: "Verification failed. Try again later" 403: description: "Verification link has expired. Please request a new one." + 500: + description: "Internal Server Error" + + /api/users/resend-verify: + post: + summary: Endpoint for resend link to verify your email + tags: + - User + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + email: + type: string + responses: + 201: + description: Check your email to verify. + 202: + description: User is already verified. Login to continue + 400: + description: Email is already used, Login to continuue 500: description: "Internal Server Error" \ No newline at end of file diff --git a/src/routes/userRoute.ts b/src/routes/userRoute.ts index b7d61964..05e8c98d 100644 --- a/src/routes/userRoute.ts +++ b/src/routes/userRoute.ts @@ -7,6 +7,7 @@ import { editUserRole, getAllUser, getOneUser, + resendVerifyLink, signupUser, userVerify, } from '../controllers/userController'; @@ -22,6 +23,7 @@ router.delete('/:id', isAuthenticated, checkUserRoles('admin'), deleteUser); router.patch('/edit/:id', isAuthenticated, multerUpload.single('profileImage'), editUser); // remove id param router.put('/role/:userId', isAuthenticated, checkUserRoles('admin'), editUserRole); router.get('/:token/verify-email', userVerify); +router.post('/resend-verify', resendVerifyLink); router.put('/deactivate/:userId', isAuthenticated, deactivateUserAccount); router.put('/activate/:userId', isAuthenticated, checkUserRoles('admin'), activateUserAccount);