Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ft-resend-verification-link #45

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
}
};
24 changes: 24 additions & 0 deletions src/docs/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions src/routes/userRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
editUserRole,
getAllUser,
getOneUser,
resendVerifyLink,
signupUser,
userVerify,
} from '../controllers/userController';
Expand All @@ -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);

Expand Down
Loading