From 2d58dd5d0c515edee168dc136a0de42f26bee320 Mon Sep 17 00:00:00 2001 From: jkarenzi Date: Thu, 27 Jun 2024 12:55:18 +0200 Subject: [PATCH] fix(email-templates): fix email templates - redesign email templates - integrate with frontend [Fixes #15] --- __tests__/authController.test.ts | 4 +- src/controllers/authController.ts | 8 +-- src/routes/authRoutes.ts | 2 +- src/utils/emailTemplates/2fa.html | 90 +++++++++++++++++++++++++++- src/utils/emailTemplates/verify.html | 90 +++++++++++++++++++++++++++- 5 files changed, 185 insertions(+), 9 deletions(-) diff --git a/__tests__/authController.test.ts b/__tests__/authController.test.ts index beccd6b..d3befcf 100644 --- a/__tests__/authController.test.ts +++ b/__tests__/authController.test.ts @@ -122,7 +122,7 @@ describe('Auth Controller Tests', () => { // email verification tests it('should verify email successfully', async () => { const token = await jwt.sign({_id:user._id}, process.env.JWT_SECRET, {expiresIn:'1h'}) - const response = await request(app).get(`/api/auth/verify_email/${token}`) + const response = await request(app).post(`/api/auth/verify_email/${token}`) expect(response.status).toBe(200); expect(response.body.message).toBe('Email verification successful'); }) @@ -145,7 +145,7 @@ describe('Auth Controller Tests', () => { it('should return a 409 when token is invalid on verify email', async () => { const token = 'fake token' - const response = await request(app).get(`/api/auth/verify_email/${token}`) + const response = await request(app).post(`/api/auth/verify_email/${token}`) expect(response.status).toBe(409); expect(response.body.message).toBe('Invalid or expired token'); }) diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index 7f174b9..5b8381e 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -45,13 +45,13 @@ const signUp = errorHandler(async (req: Request, res: Response) => { const savedUser = await newUser.save(); const token = await jwt.sign({ _id: savedUser._id }, jwtSecret, { expiresIn: '1h' }); - const verifyLink = `${process.env.APP_URL}/api/auth/verify_email/${token}` + const verifyLink = `${process.env.FRONTEND_URL}/auth/verify/${token}` await sendEmail('verify', formData.email, {name:formData.fullName, link:verifyLink}) return res .status(201) - .json({ status: 'success', message: 'Signup successful!' }); + .json({ status: 'success', message: 'Signup successful!', data:{email:formData.email} }); }); const login = errorHandler(async (req: Request, res: Response) => { @@ -89,7 +89,7 @@ const login = errorHandler(async (req: Request, res: Response) => { const twoFactorCode = Math.floor(100000 + Math.random() * 900000); await User.findByIdAndUpdate(user._id, {'twoFactorAuth.code':twoFactorCode}) await sendEmail('2fa',user.email,{name:user.fullName, code:twoFactorCode.toString()}) - return res.status(200).json({status:'success',message:'A Two Factor Auth Code has been sent to your email'}) + return res.status(200).json({status:'success',message:'A Two Factor Auth Code has been sent to your email',data:{_id:user._id, email:user.email}}) } const token = await jwt.sign({ user }, jwtSecret, { expiresIn: '1h' }); @@ -120,7 +120,7 @@ const requestVerifyLink = errorHandler(async (req:Request, res:Response) => { } const token = await jwt.sign({ _id: user._id }, jwtSecret, { expiresIn: '1h' }); - const verifyLink = `${process.env.APP_URL}/api/auth/verify_email/${token}` + const verifyLink = `${process.env.FRONTEND_URL}/auth/verify/${token}` await sendEmail('verify', user.email, {name:user.fullName, link:verifyLink}) diff --git a/src/routes/authRoutes.ts b/src/routes/authRoutes.ts index 7e26483..d893527 100644 --- a/src/routes/authRoutes.ts +++ b/src/routes/authRoutes.ts @@ -14,7 +14,7 @@ const authRouter = Router(); authRouter.post('/signup', signUp); authRouter.post('/login', login); -authRouter.get('/verify_email/:token', verifyEmail) +authRouter.post('/verify_email/:token', verifyEmail) authRouter.post('/verify_code/:userId', verifyTwoFactorCode) authRouter.post('/request_new_link', authenticateToken, requestVerifyLink) authRouter.post('/request_new_code/:userId', requestTwoFactorCode) diff --git a/src/utils/emailTemplates/2fa.html b/src/utils/emailTemplates/2fa.html index cf6d2d3..27c1c62 100644 --- a/src/utils/emailTemplates/2fa.html +++ b/src/utils/emailTemplates/2fa.html @@ -4,8 +4,96 @@ 2fa + + -

Hi {{ name }}, your code is {{ code }}

+
+

TaskMaster

+
+

Login verification

+

Your verification code

+

{{code}}

+

+ The above code is for personal use only. Please do not share it with anyone. If you did not request this code, please reset your password as soon as possible or contact customer support +

+

+ Best regards,
+ TaskMaster Team +

+
+ +
\ No newline at end of file diff --git a/src/utils/emailTemplates/verify.html b/src/utils/emailTemplates/verify.html index 3883f84..1f7976b 100644 --- a/src/utils/emailTemplates/verify.html +++ b/src/utils/emailTemplates/verify.html @@ -4,8 +4,96 @@ Verify + + -

Hi {{ name }}, your link is {{ link }}

+
+

TaskMaster

+
+

+ Hi {{name}},
+ Welcome to TaskMaster! We're excited to have you on board. + To complete your registration, please verify your email address by clicking the link below: +

+ Verify your email +

If you are not able to access the above link, you can copy the link below and paste it directly into your browser

+

{{link}}

+

+ If you did not create an account with TaskMaster, please ignore this email. + Thank you for joining us! +

+

+ Best regards,
+ TaskMaster Team +

+
+ +
\ No newline at end of file