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

Fix: Email templates #16

Merged
merged 1 commit into from
Jul 1, 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
4 changes: 2 additions & 2 deletions __tests__/authController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
Expand All @@ -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');
})
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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' });
Expand Down Expand Up @@ -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})

Expand Down
2 changes: 1 addition & 1 deletion src/routes/authRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
90 changes: 89 additions & 1 deletion src/utils/emailTemplates/2fa.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,96 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2fa</title>
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: 'Open Sans', sans-serif;
}

.big-container {
width: 100%;
height: 100vh;
background-color: #1A1C1E;
color: white;
padding: 2rem;
box-sizing: border-box;
}

.title {
color: #586AEA;
margin: 0;
text-align: center;
}

.p-container {
width: 100%;
}

p {
width: 100%;
text-align: left;
margin: 0;
padding: 0;
margin-top: 2rem;
color: white;
}

.title-1{
margin: 0;
padding: 0;
font-weight: 600;
font-size: x-large;
margin-top: 2rem;
color: white;
}

.title-2{
margin: 0;
padding: 0;
font-weight: 400;
margin-top: 2rem;
color: white;
}

.code {
color: #586AEA;
margin: 0;
padding: 0;
margin-top: 1rem;
}

.footer {
border-top: 1px solid #666666;
color: #aeaeae;
height: 3rem;
width: 100%;
margin-top: 5rem;
padding-top: 0.5rem;
font-size: small;
}
</style>
</head>
<body>
<h2>Hi {{ name }}, your code is {{ code }}</h2>
<div class="big-container">
<h2 class="title">TaskMaster</h2>
<div class="p-container">
<h2 class="title-1">Login verification</h2>
<h3 class="title-2">Your verification code</h3>
<h2 class="code">{{code}}</h2>
<p>
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
</p>
<p>
Best regards,<br/>
TaskMaster Team
</p>
</div>
<div class="footer">
Copyright@2024. All rights reserved
</div>
</div>
</body>
</html>
90 changes: 89 additions & 1 deletion src/utils/emailTemplates/verify.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,96 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Verify</title>
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: 'Open Sans', sans-serif;
box-sizing: border-box;
}

.big-container {
width: 100%;
height: 100vh;
background-color: #1A1C1E;
color: white;
padding: 2rem;
box-sizing: border-box;
}

.title {
color: #586AEA;
margin: 0;
text-align: center;
}

.p-container {
width: 100%;
gap: 2rem;
}

p {
width: 100%;
text-align: left;
margin: 0;
padding: 0;
margin-top: 2rem;
}

.link {
background-color: #586AEA;
color: white !important;
padding: 12px;
display: block;
text-align: center;
border-radius: 6px;
margin-top: 2rem;
text-align: center;
text-decoration: none;
}

.link2{
color: #586AEA !important;
margin-top: 2rem;
}

.footer {
border-top: 1px solid #666666;
color: #aeaeae;
height: 3rem;
width: 100%;
margin-top: 5rem;
padding-top: 0.5rem;
font-size: small;
}
</style>
</head>
<body>
<h2>Hi {{ name }}, your link is {{ link }}</h2>
<div class="big-container">
<h2 class="title">TaskMaster</h2>
<div class="p-container">
<p style="color: white;">
Hi {{name}},<br/>
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:
</p>
<a href="{{link}}" class="link">Verify your email</a>
<p style="color: white;">If you are not able to access the above link, you can copy the link below and paste it directly into your browser</p>
<p class="link2">{{link}}</p>
<p style="color: white;">
If you did not create an account with TaskMaster, please ignore this email.
Thank you for joining us!
</p>
<p style="color: white;">
Best regards,<br/>
TaskMaster Team
</p>
</div>
<div class="footer">
Copyright@2024. All rights reserved
</div>
</div>
</body>
</html>
Loading